@@ -77,14 +77,21 @@ sequenceDiagram
7777
7878## Example: Defining a Simple Tool
7979
80- ``` ts
80+ ``` ts lines=7
8181import { McpServer } from ' @modelcontextprotocol/sdk/server/mcp.js'
8282
8383const server = new McpServer (
8484 { name: ' hello-world-server' , version: ' 1.0.0' },
85- { capabilities: { tools: {} }, instructions: ' A simple hello world server.' },
85+ {
86+ capabilities: {
87+ // add the tools object to explicitly declare that this server supports tools
88+ tools: {},
89+ },
90+ instructions: ' A simple hello world server.' ,
91+ },
8692)
8793
94+ // register a tool with the server
8895server .registerTool (
8996 // llm-facing name
9097 ' hello' ,
@@ -93,10 +100,15 @@ server.registerTool(
93100 title: ' Hello' ,
94101 // llm-facing description (clients could also display this to the user)
95102 description: ' Say hello' ,
103+ // add a schema to validate the input
104+ inputSchema: {
105+ // the input description is llm-facing, (the user may see it as well)
106+ name: z .string ().describe (' The name to say hello to' ),
107+ },
96108 },
97- async () => {
109+ async ({ name } ) => {
98110 return {
99- content: [{ type: ' text' , text: ' Hello, world! ' }],
111+ content: [{ type: ' text' , text: ` Hello, ${ name }! ` }],
100112 }
101113 },
102114)
@@ -111,7 +123,9 @@ Example client request:
111123 "method" : " tools/call" ,
112124 "params" : {
113125 "name" : " hello" ,
114- "arguments" : {}
126+ "arguments" : {
127+ "name" : " Kody"
128+ }
115129 }
116130}
117131```
@@ -126,7 +140,7 @@ Example server response:
126140 "content" : [
127141 {
128142 "type" : " text" ,
129- "text" : " Hello, world !"
143+ "text" : " Hello, Kody !"
130144 }
131145 ],
132146 "isError" : false
0 commit comments