Skip to content

Commit fb4d07f

Browse files
authored
Merge pull request #698 from RishiNandha/main
Added Sampling Example to README
2 parents 5e20ee1 + a8fa0b3 commit fb4d07f

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

README.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
- [Tools](#tools)
1313
- [Prompts](#prompts)
1414
- [Completions](#completions)
15+
- [Sampling](#sampling)
1516
- [Running Your Server](#running-your-server)
1617
- [stdio](#stdio)
1718
- [Streamable HTTP](#streamable-http)
@@ -384,6 +385,68 @@ import { getDisplayName } from "@modelcontextprotocol/sdk/shared/metadataUtils.j
384385
const displayName = getDisplayName(tool);
385386
```
386387

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+
387450
## Running Your Server
388451

389452
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:
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
// Run with: npx tsx src/examples/server/toolWithSampleServer.ts
3+
4+
import { McpServer } from "../../server/mcp.js";
5+
import { StdioServerTransport } from "../../server/stdio.js";
6+
import { z } from "zod";
7+
8+
const mcpServer = new McpServer({
9+
name: "tools-with-sample-server",
10+
version: "1.0.0",
11+
});
12+
13+
// Tool that uses LLM sampling to summarize any text
14+
mcpServer.registerTool(
15+
"summarize",
16+
{
17+
description: "Summarize any text using an LLM",
18+
inputSchema: {
19+
text: z.string().describe("Text to summarize"),
20+
},
21+
},
22+
async ({ text }) => {
23+
// Call the LLM through MCP sampling
24+
const response = await mcpServer.server.createMessage({
25+
messages: [
26+
{
27+
role: "user",
28+
content: {
29+
type: "text",
30+
text: `Please summarize the following text concisely:\n\n${text}`,
31+
},
32+
},
33+
],
34+
maxTokens: 500,
35+
});
36+
37+
return {
38+
content: [
39+
{
40+
type: "text",
41+
text: response.content.type === "text" ? response.content.text : "Unable to generate summary",
42+
},
43+
],
44+
};
45+
}
46+
);
47+
48+
async function main() {
49+
const transport = new StdioServerTransport();
50+
await mcpServer.connect(transport);
51+
console.log("MCP server is running...");
52+
}
53+
54+
main().catch((error) => {
55+
console.error("Server error:", error);
56+
process.exit(1);
57+
});

0 commit comments

Comments
 (0)