Skip to content

Commit 3d6acd3

Browse files
committed
add an example and update readme
1 parent ca9924b commit 3d6acd3

File tree

2 files changed

+111
-23
lines changed

2 files changed

+111
-23
lines changed

README.md

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -385,35 +385,66 @@ const displayName = getDisplayName(tool);
385385

386386
### Sampling
387387

388-
MCP servers can also request MCP client LLMs for responses. Below is an example of a sampling request sent just after connecting to the Client
388+
MCP servers can request LLM completions from connected clients that support sampling.
389389

390390
```typescript
391-
// Result sent back from LLM follow the CreateMessageSchema
392-
import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js";
393-
394-
// Async Function to send a sampling request to the LLM at top-level
395-
async function samplingExample(server: McpServer): Promise<CreateMessageResult> {
396-
const samplingText = "Example Sampling Prompt";
397-
const result = await McpServer.server.createMessage(
398-
{
399-
messages : [{
400-
role: "user",
401-
content: {
402-
text: samplingText,
403-
type: "text"
404-
}
405-
}],
406-
maxTokens: 1000
407-
}
408-
);
409-
return result;
391+
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
392+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
393+
import { z } from "zod";
394+
395+
const mcpServer = new McpServer({
396+
name: "tools-with-sample-server",
397+
version: "1.0.0",
398+
});
399+
400+
// Tool that uses LLM sampling to summarize any text
401+
mcpServer.registerTool(
402+
"summarize",
403+
{
404+
description: "Summarize any text using an LLM",
405+
inputSchema: {
406+
text: z.string().describe("Text to summarize"),
407+
},
408+
},
409+
async ({ text }) => {
410+
// Call the LLM through MCP sampling
411+
const response = await mcpServer.server.createMessage({
412+
messages: [
413+
{
414+
role: "user",
415+
content: {
416+
type: "text",
417+
text: `Please summarize the following text concisely:\n\n${text}`,
418+
},
419+
},
420+
],
421+
maxTokens: 500,
422+
});
423+
424+
return {
425+
content: [
426+
{
427+
type: "text",
428+
text: response.content.type === "text" ? response.content.text : "Unable to generate summary",
429+
},
430+
],
431+
};
432+
}
433+
);
434+
435+
async function main() {
436+
const transport = new StdioServerTransport();
437+
await mcpServer.connect(transport);
438+
console.log("MCP server is running...");
410439
}
411440

412-
// Sampling request just after connecting to MCP Client
413-
server.connect(transport);
414-
samplingExample(server);
441+
main().catch((error) => {
442+
console.error("Server error:", error);
443+
process.exit(1);
444+
});
415445
```
416446

447+
417448
## Running Your Server
418449

419450
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 "@modelcontextprotocol/sdk/server/mcp.js";
5+
import { StdioServerTransport } from "@modelcontextprotocol/sdk/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)