11import { z } from "zod/v3" ;
22import { zodToJsonSchema } from "zod-to-json-schema" ;
33import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js" ;
4- import { CallToolRequestSchema , ListToolsRequestSchema , McpError , ErrorCode } from "@modelcontextprotocol/sdk/types.js" ;
5-
4+ import {
5+ CallToolRequestSchema ,
6+ ListToolsRequestSchema ,
7+ McpError ,
8+ ErrorCode ,
9+ } from "@modelcontextprotocol/sdk/types.js" ;
610
711type ToolCallback = ( args : any , extra ?: any ) => Promise < any > | any ;
812
@@ -16,67 +20,75 @@ interface ToolDef {
1620export class ToolRegistry {
1721 private tools : Map < string , ToolDef > = new Map ( ) ;
1822
19- tool ( name : string , description : string , inputSchema : any , callback : ToolCallback ) {
23+ tool (
24+ name : string ,
25+ description : string ,
26+ inputSchema : any ,
27+ callback : ToolCallback ,
28+ ) {
2029 this . tools . set ( name , {
2130 name,
2231 description,
2332 inputSchema : z . object ( inputSchema ) ,
24- callback
33+ callback,
2534 } ) ;
2635 }
2736
2837 apply ( server : McpServer ) {
29-
30-
3138 const srv = server . server ;
3239
3340 srv . setRequestHandler ( ListToolsRequestSchema , async ( ) => {
3441 return {
35- tools : Array . from ( this . tools . values ( ) ) . map ( t => {
42+ tools : Array . from ( this . tools . values ( ) ) . map ( ( t ) => {
3643 const jsonSchema = zodToJsonSchema ( t . inputSchema , {
37- target : "jsonSchema2019-09"
44+ target : "jsonSchema2019-09" ,
3845 } ) as Record < string , unknown > ;
3946
40-
41- if ( jsonSchema && typeof jsonSchema === 'object' ) {
42-
43-
44-
45-
47+ if ( jsonSchema && typeof jsonSchema === "object" ) {
4648 delete jsonSchema . $schema ;
4749 }
4850
4951 return {
5052 name : t . name ,
5153 description : t . description ,
52- inputSchema : jsonSchema
54+ inputSchema : jsonSchema ,
5355 } ;
54- } )
56+ } ) ,
5557 } ;
5658 } ) ;
5759
58- srv . setRequestHandler ( CallToolRequestSchema , async ( req : any , extra : any ) => {
59- const name = req . params . name ;
60- const tool = this . tools . get ( name ) ;
61- if ( ! tool ) {
62- throw new McpError ( ErrorCode . MethodNotFound , `Tool not found: ${ name } ` ) ;
63- }
64-
65-
66- const args = req . params . arguments || { } ;
67- const parse = await tool . inputSchema . safeParseAsync ( args ) ;
68- if ( ! parse . success ) {
69- throw new McpError ( ErrorCode . InvalidParams , `Invalid arguments: ${ parse . error . message } ` ) ;
70- }
71-
72- try {
73- return await tool . callback ( parse . data , extra ) ;
74- } catch ( err : any ) {
75- return {
76- content : [ { type : "text" , text : `Error: ${ err . message } ` } ] ,
77- isError : true
78- } ;
79- }
80- } ) ;
60+ srv . setRequestHandler (
61+ CallToolRequestSchema ,
62+ async ( req : any , extra : any ) => {
63+ const name = req . params . name ;
64+ const tool = this . tools . get ( name ) ;
65+ if ( ! tool ) {
66+ throw new McpError (
67+ ErrorCode . MethodNotFound ,
68+ `Tool not found: ${ name } ` ,
69+ ) ;
70+ }
71+
72+ const args = req . params . arguments || { } ;
73+ const parse = await tool . inputSchema . safeParseAsync ( args ) ;
74+ if ( ! parse . success ) {
75+ throw new McpError (
76+ ErrorCode . InvalidParams ,
77+ `Invalid arguments: ${ parse . error . message } ` ,
78+ ) ;
79+ }
80+
81+ try {
82+ return await tool . callback ( parse . data , extra ) ;
83+ } catch ( err : any ) {
84+ return {
85+ content : [
86+ { type : "text" , text : `Error: ${ err . message } ` } ,
87+ ] ,
88+ isError : true ,
89+ } ;
90+ }
91+ } ,
92+ ) ;
8193 }
8294}
0 commit comments