clickup-mcp-server
Version:
A Model Context Protocol (MCP) server that provides a standardized interface for AI assistants to interact with the ClickUp API
114 lines • 4.2 kB
JavaScript
import { ListResourcesRequestSchema, ListResourceTemplatesRequestSchema, ReadResourceRequestSchema } from '@modelcontextprotocol/sdk/types.js';
/**
* ResourceManager is a centralized manager for MCP resources.
* It provides a simpler interface for registering resources and templates with the MCP server.
*/
export class ResourceManager {
constructor() {
this.resources = [];
this.resourceTemplates = [];
this.readHandlers = [];
}
/**
* Adds a resource to the manager
* @param resource The resource definition
*/
addResource(resource) {
// Check if resource with this URI already exists
const existingIndex = this.resources.findIndex(r => r.uri === resource.uri);
if (existingIndex >= 0) {
// Replace existing resource
this.resources[existingIndex] = resource;
}
else {
// Add new resource
this.resources.push(resource);
}
}
/**
* Adds multiple resources to the manager
* @param resources Array of resource definitions
*/
addResources(resources) {
for (const resource of resources) {
this.addResource(resource);
}
}
/**
* Adds a resource template to the manager
* @param template The resource template definition
*/
addResourceTemplate(template) {
// Check if template with this URI pattern already exists
const existingIndex = this.resourceTemplates.findIndex(t => t.uriTemplate === template.uriTemplate);
if (existingIndex >= 0) {
// Replace existing template
this.resourceTemplates[existingIndex] = template;
}
else {
// Add new template
this.resourceTemplates.push(template);
}
}
/**
* Adds multiple resource templates to the manager
* @param templates Array of resource template definitions
*/
addResourceTemplates(templates) {
for (const template of templates) {
this.addResourceTemplate(template);
}
}
/**
* Registers a handler for resource reads based on a URI pattern
* @param pattern A regular expression to match URIs
* @param handler A function that handles resource requests matching the pattern
*/
registerReadHandler(pattern, handler) {
this.readHandlers.push({ pattern, handler });
}
/**
* Registers all resources and handlers with the MCP server
* @param server The MCP server instance
*/
registerWithServer(server) {
// Register resource list handler
server.setRequestHandler(ListResourcesRequestSchema, async () => {
return {
resources: this.resources,
};
});
// Register resource template list handler
server.setRequestHandler(ListResourceTemplatesRequestSchema, async () => {
return {
resourceTemplates: this.resourceTemplates,
};
});
// Register resource read handler
server.setRequestHandler(ReadResourceRequestSchema, async (request) => {
const uri = request.params.uri;
// Try each handler to see if it matches the URI
for (const { pattern, handler } of this.readHandlers) {
const match = uri.match(pattern);
if (match) {
// Extract the parameters from the match
const params = match.slice(1);
// Call the handler with the extracted parameters
try {
const result = await handler(params);
return result;
}
catch (error) {
console.error(`Error handling resource read: ${error.message}`, error);
throw error;
}
}
}
// If no handler matched, return empty object
return {};
});
}
}
// Export a singleton instance
export const resourceManager = new ResourceManager();
//# sourceMappingURL=resource-manager.js.map