From 7a64f974936fd27faf063df8f9e952740bce9ad4 Mon Sep 17 00:00:00 2001 From: Rishi Nandha Vanchi <49914358+RishiNandha@users.noreply.github.com> Date: Wed, 25 Jun 2025 20:08:15 +0530 Subject: [PATCH 1/5] Add: Sampling Example to README --- README.md | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/README.md b/README.md index aa8f9304..0d8d8f7c 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ - [Tools](#tools) - [Prompts](#prompts) - [Completions](#completions) + - [Sampling](#sampling) - [Running Your Server](#running-your-server) - [stdio](#stdio) - [Streamable HTTP](#streamable-http) @@ -382,6 +383,37 @@ import { getDisplayName } from "@modelcontextprotocol/sdk/shared/metadataUtils.j const displayName = getDisplayName(tool); ``` +### Sampling + +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 + +```typescript +// Result sent back from LLM follow the CreateMessageSchema +import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js"; + +// Async Function to send a sampling request to the LLM at top-level +async function samplingExample(server: McpServer): Promise { + const samplingText = "Text prompt to send to LLM"; + const result = await McpServer.server.createMessage( + { + messages : [{ + role: "user", + content: { + text: samplingText, + type: "text" + } + }], + maxTokens: 1000 + } + ); + return result; +} + +// Sampling request just after connecting to MCP Client +server.connect(transport); +samplingExample(server); +``` + ## Running Your Server 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: From b232faf7987852cd063ec1bda03cba533b5e324e Mon Sep 17 00:00:00 2001 From: Rishi Nandha Vanchi <49914358+RishiNandha@users.noreply.github.com> Date: Wed, 25 Jun 2025 20:09:54 +0530 Subject: [PATCH 2/5] Fixed Tabs --- README.md | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index 0d8d8f7c..4f7ed106 100644 --- a/README.md +++ b/README.md @@ -393,20 +393,20 @@ import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js"; // Async Function to send a sampling request to the LLM at top-level async function samplingExample(server: McpServer): Promise { - const samplingText = "Text prompt to send to LLM"; - const result = await McpServer.server.createMessage( - { - messages : [{ - role: "user", - content: { - text: samplingText, - type: "text" - } - }], - maxTokens: 1000 - } - ); - return result; + const samplingText = "Text prompt to send to LLM"; + const result = await McpServer.server.createMessage( + { + messages : [{ + role: "user", + content: { + text: samplingText, + type: "text" + } + }], + maxTokens: 1000 + } + ); + return result; } // Sampling request just after connecting to MCP Client From ca9924b20b52014d0b0e8a2fed143caf2d2e37c5 Mon Sep 17 00:00:00 2001 From: Rishi Nandha Vanchi <49914358+RishiNandha@users.noreply.github.com> Date: Wed, 25 Jun 2025 20:10:57 +0530 Subject: [PATCH 3/5] Changed example --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 4f7ed106..5e2afd06 100644 --- a/README.md +++ b/README.md @@ -393,7 +393,7 @@ import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js"; // Async Function to send a sampling request to the LLM at top-level async function samplingExample(server: McpServer): Promise { - const samplingText = "Text prompt to send to LLM"; + const samplingText = "Example Sampling Prompt"; const result = await McpServer.server.createMessage( { messages : [{ From 3d6acd3d9ac50eaed334b5c96e16ee033f2abbd8 Mon Sep 17 00:00:00 2001 From: ihrpr Date: Mon, 30 Jun 2025 16:23:19 +0100 Subject: [PATCH 4/5] add an example and update readme --- README.md | 75 +++++++++++++++------ src/examples/server/toolWithSampleServer.ts | 57 ++++++++++++++++ 2 files changed, 110 insertions(+), 22 deletions(-) create mode 100644 src/examples/server/toolWithSampleServer.ts diff --git a/README.md b/README.md index 5e2afd06..f8143501 100644 --- a/README.md +++ b/README.md @@ -385,35 +385,66 @@ const displayName = getDisplayName(tool); ### Sampling -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 +MCP servers can request LLM completions from connected clients that support sampling. ```typescript -// Result sent back from LLM follow the CreateMessageSchema -import {CreateMessageResult} from "@modelcontextprotocol/sdk/types.js"; +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { z } from "zod"; -// Async Function to send a sampling request to the LLM at top-level -async function samplingExample(server: McpServer): Promise { - const samplingText = "Example Sampling Prompt"; - const result = await McpServer.server.createMessage( - { - messages : [{ - role: "user", - content: { - text: samplingText, - type: "text" - } - }], - maxTokens: 1000 - } - ); - return result; +const mcpServer = new McpServer({ + name: "tools-with-sample-server", + version: "1.0.0", +}); + +// Tool that uses LLM sampling to summarize any text +mcpServer.registerTool( + "summarize", + { + description: "Summarize any text using an LLM", + inputSchema: { + text: z.string().describe("Text to summarize"), + }, + }, + async ({ text }) => { + // Call the LLM through MCP sampling + const response = await mcpServer.server.createMessage({ + messages: [ + { + role: "user", + content: { + type: "text", + text: `Please summarize the following text concisely:\n\n${text}`, + }, + }, + ], + maxTokens: 500, + }); + + return { + content: [ + { + type: "text", + text: response.content.type === "text" ? response.content.text : "Unable to generate summary", + }, + ], + }; + } +); + +async function main() { + const transport = new StdioServerTransport(); + await mcpServer.connect(transport); + console.log("MCP server is running..."); } -// Sampling request just after connecting to MCP Client -server.connect(transport); -samplingExample(server); +main().catch((error) => { + console.error("Server error:", error); + process.exit(1); +}); ``` + ## Running Your Server 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: diff --git a/src/examples/server/toolWithSampleServer.ts b/src/examples/server/toolWithSampleServer.ts new file mode 100644 index 00000000..7e92f33b --- /dev/null +++ b/src/examples/server/toolWithSampleServer.ts @@ -0,0 +1,57 @@ + +// Run with: npx tsx src/examples/server/toolWithSampleServer.ts + +import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; +import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { z } from "zod"; + +const mcpServer = new McpServer({ + name: "tools-with-sample-server", + version: "1.0.0", +}); + +// Tool that uses LLM sampling to summarize any text +mcpServer.registerTool( + "summarize", + { + description: "Summarize any text using an LLM", + inputSchema: { + text: z.string().describe("Text to summarize"), + }, + }, + async ({ text }) => { + // Call the LLM through MCP sampling + const response = await mcpServer.server.createMessage({ + messages: [ + { + role: "user", + content: { + type: "text", + text: `Please summarize the following text concisely:\n\n${text}`, + }, + }, + ], + maxTokens: 500, + }); + + return { + content: [ + { + type: "text", + text: response.content.type === "text" ? response.content.text : "Unable to generate summary", + }, + ], + }; + } +); + +async function main() { + const transport = new StdioServerTransport(); + await mcpServer.connect(transport); + console.log("MCP server is running..."); +} + +main().catch((error) => { + console.error("Server error:", error); + process.exit(1); +}); \ No newline at end of file From a8fa0b342ac0411d0827e48820400e5d048baa45 Mon Sep 17 00:00:00 2001 From: ihrpr Date: Mon, 30 Jun 2025 16:25:26 +0100 Subject: [PATCH 5/5] build --- src/examples/server/toolWithSampleServer.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/examples/server/toolWithSampleServer.ts b/src/examples/server/toolWithSampleServer.ts index 7e92f33b..44e5cecb 100644 --- a/src/examples/server/toolWithSampleServer.ts +++ b/src/examples/server/toolWithSampleServer.ts @@ -1,8 +1,8 @@ // Run with: npx tsx src/examples/server/toolWithSampleServer.ts -import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js"; -import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js"; +import { McpServer } from "../../server/mcp.js"; +import { StdioServerTransport } from "../../server/stdio.js"; import { z } from "zod"; const mcpServer = new McpServer({