|
12 | 12 | - [Tools](#tools)
|
13 | 13 | - [Prompts](#prompts)
|
14 | 14 | - [Completions](#completions)
|
| 15 | + - [Sampling](#sampling) |
15 | 16 | - [Running Your Server](#running-your-server)
|
16 | 17 | - [stdio](#stdio)
|
17 | 18 | - [Streamable HTTP](#streamable-http)
|
@@ -384,6 +385,68 @@ import { getDisplayName } from "@modelcontextprotocol/sdk/shared/metadataUtils.j
|
384 | 385 | const displayName = getDisplayName(tool);
|
385 | 386 | ```
|
386 | 387 |
|
| 388 | +### Sampling |
| 389 | + |
| 390 | +MCP servers can request LLM completions from connected clients that support sampling. |
| 391 | + |
| 392 | +```typescript |
| 393 | +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; |
| 394 | +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; |
| 395 | +import { z } from "zod"; |
| 396 | + |
| 397 | +const mcpServer = new McpServer({ |
| 398 | + name: "tools-with-sample-server", |
| 399 | + version: "1.0.0", |
| 400 | +}); |
| 401 | + |
| 402 | +// Tool that uses LLM sampling to summarize any text |
| 403 | +mcpServer.registerTool( |
| 404 | + "summarize", |
| 405 | + { |
| 406 | + description: "Summarize any text using an LLM", |
| 407 | + inputSchema: { |
| 408 | + text: z.string().describe("Text to summarize"), |
| 409 | + }, |
| 410 | + }, |
| 411 | + async ({ text }) => { |
| 412 | + // Call the LLM through MCP sampling |
| 413 | + const response = await mcpServer.server.createMessage({ |
| 414 | + messages: [ |
| 415 | + { |
| 416 | + role: "user", |
| 417 | + content: { |
| 418 | + type: "text", |
| 419 | + text: `Please summarize the following text concisely:\n\n${text}`, |
| 420 | + }, |
| 421 | + }, |
| 422 | + ], |
| 423 | + maxTokens: 500, |
| 424 | + }); |
| 425 | + |
| 426 | + return { |
| 427 | + content: [ |
| 428 | + { |
| 429 | + type: "text", |
| 430 | + text: response.content.type === "text" ? response.content.text : "Unable to generate summary", |
| 431 | + }, |
| 432 | + ], |
| 433 | + }; |
| 434 | + } |
| 435 | +); |
| 436 | + |
| 437 | +async function main() { |
| 438 | + const transport = new StdioServerTransport(); |
| 439 | + await mcpServer.connect(transport); |
| 440 | + console.log("MCP server is running..."); |
| 441 | +} |
| 442 | + |
| 443 | +main().catch((error) => { |
| 444 | + console.error("Server error:", error); |
| 445 | + process.exit(1); |
| 446 | +}); |
| 447 | +``` |
| 448 | + |
| 449 | + |
387 | 450 | ## Running Your Server
|
388 | 451 |
|
389 | 452 | MCP servers in TypeScript need to be connected to a transport to communicate with clients. How you start the server depends on the choice of transport:
|
|
0 commit comments