Merge pull request #3252 from dotsetgreg/fix/session-resource-reregistration

fix(everything): allow re-registration of session resources
This commit is contained in:
Ola Hungerford 2026-03-07 12:32:28 -07:00 • committed by GitHub
commit ad3e660cc1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,6 +1,13 @@
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; import { McpServer, RegisteredResource } from "@modelcontextprotocol/sdk/server/mcp.js";
import { Resource, ResourceLink } from "@modelcontextprotocol/sdk/types.js"; import { Resource, ResourceLink } from "@modelcontextprotocol/sdk/types.js";
/**
* Tracks registered session resources by URI to allow updating/removing on re-registration.
* This prevents "Resource already registered" errors when a tool creates a resource
* with the same URI multiple times during a session.
*/
const registeredResources = new Map<string, RegisteredResource>();
/** /**
* Generates a session-scoped resource URI string based on the provided resource name. * Generates a session-scoped resource URI string based on the provided resource name.
* *
@ -47,17 +54,27 @@ export const registerSessionResource = (
blob: payload, blob: payload,
}; };
// Check if a resource with this URI is already registered and remove it
const existingResource = registeredResources.get(uri);
if (existingResource) {
existingResource.remove();
registeredResources.delete(uri);
}
// Register file resource // Register file resource
server.registerResource( const registeredResource = server.registerResource(
name, name,
uri, uri,
{ mimeType, description, title, annotations, icons, _meta }, { mimeType, description, title, annotations, icons, _meta },
async (uri) => { async () => {
return { return {
contents: [resourceContent], contents: [resourceContent],
}; };
} }
); );
// Track the registered resource for potential future removal
registeredResources.set(uri, registeredResource);
return { type: "resource_link", ...resource }; return { type: "resource_link", ...resource };
}; };