amazon-seller-mcp
Version:
Model Context Protocol (MCP) client for Amazon Selling Partner API
89 lines • 2.87 kB
JavaScript
/**
* Tool registration for the Amazon Seller MCP Server
*/
// Internal imports
import { handleToolError } from './error-handler.js';
import { warn, error, info } from '../utils/logger.js';
/**
* Tool registration manager
*/
export class ToolRegistrationManager {
server;
registeredTools = new Set();
toolHandlers = new Map();
/**
* Creates a new tool registration manager
* @param server MCP server instance
*/
constructor(server) {
this.server = server;
}
/**
* Registers a tool with the MCP server
*
* @param name Tool name
* @param options Tool registration options
* @param handler Tool handler function
* @returns True if the tool was registered, false if it was already registered
*/
registerTool(name, options, handler) {
// Check if the tool is already registered
if (this.registeredTools.has(name)) {
warn(`Tool '${name}' is already registered`);
return false;
}
// Register the tool with the MCP server
this.server.registerTool(name, {
title: options.title,
description: options.description,
inputSchema: ('shape' in options.inputSchema
? options.inputSchema.shape
: options.inputSchema),
}, async (input) => {
try {
const result = await handler(input);
return result;
}
catch (err) {
error(`Error handling tool '${name}':`, { error: err });
// Use the error handler to create a standardized error response
return handleToolError(err);
}
});
// Add the tool to the set of registered tools
this.registeredTools.add(name);
// Store the handler for direct access (useful for testing)
this.toolHandlers.set(name, handler);
info(`Registered tool '${name}'`);
return true;
}
/**
* Gets the list of registered tool names
* @returns Array of registered tool names
*/
getRegisteredTools() {
return Array.from(this.registeredTools);
}
/**
* Checks if a tool is registered
* @param name Tool name
* @returns True if the tool is registered, false otherwise
*/
isToolRegistered(name) {
return this.registeredTools.has(name);
}
/**
* Gets a tool handler for direct invocation (primarily for testing)
* @param name Tool name
* @returns Tool handler function
* @throws Error if the tool is not registered
*/
getToolHandler(name) {
const handler = this.toolHandlers.get(name);
if (!handler) {
throw new Error(`Tool '${name}' is not registered`);
}
return handler;
}
}
//# sourceMappingURL=tools.js.map