@modelcontextprotocol/server-everything
Version:
MCP server that exercises all the features of the MCP protocol
63 lines (62 loc) • 2.75 kB
JavaScript
/**
* Tracks registered session resources per server, 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, without touching another
* session's server that registered the same URI (#4808).
*/
const registeredResources = new WeakMap();
/**
* Generates a session-scoped resource URI string based on the provided resource name.
*
* @param {string} name - The name of the resource to create a URI for.
* @returns {string} The formatted session resource URI.
*/
export const getSessionResourceURI = (name) => {
return `demo://resource/session/${name}`;
};
/**
* Registers a session-scoped resource with the provided server and returns a resource link.
*
* The registered resource is available during the life of the session only; it is not otherwise persisted.
*
* @param {McpServer} server - The server instance responsible for handling the resource registration.
* @param {Resource} resource - The resource object containing metadata such as URI, name, description, and mimeType.
* @param {"text"|"blob"} type
* @param payload
* @returns {ResourceLink} An object representing the resource link, with associated metadata.
*/
export const registerSessionResource = (server, resource, type, payload) => {
// Destructure resource
const { uri, name, mimeType, description, title, annotations, icons, _meta } = resource;
// Prepare the resource content to return
// See https://modelcontextprotocol.io/specification/2025-11-25/server/resources#resource-contents
const resourceContent = type === "text"
? {
uri: uri.toString(),
mimeType,
text: payload,
}
: {
uri: uri.toString(),
mimeType,
blob: payload,
};
const serverResources = registeredResources.get(server) ?? new Map();
registeredResources.set(server, serverResources);
// Check if a resource with this URI is already registered on this server and remove it
const resourceKey = uri.toString();
const existingResource = serverResources.get(resourceKey);
if (existingResource) {
existingResource.remove();
serverResources.delete(resourceKey);
}
// Register file resource
const registeredResource = server.registerResource(name, uri, { mimeType, description, title, annotations, icons, _meta }, async () => {
return {
contents: [resourceContent],
};
});
// Track the registered resource for potential future removal
serverResources.set(resourceKey, registeredResource);
return { type: "resource_link", ...resource };
};