Merge pull request #698 from RishiNandha/main

Added Sampling Example to README
This commit is contained in:
Inna Harper 2025-06-30 16:31:42 +01:00 • committed by GitHub
commit fb4d07f0ee
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -12,6 +12,7 @@
- [Tools](#tools)
- [Prompts](#prompts)
- [Completions](#completions)
- [Sampling](#sampling)
- [Running Your Server](#running-your-server)
- [stdio](#stdio)
- [Streamable HTTP](#streamable-http)
@ -384,6 +385,68 @@ import { getDisplayName } from "@modelcontextprotocol/sdk/shared/metadataUtils.j
const displayName = getDisplayName(tool);
```
### Sampling
MCP servers can request LLM completions from connected clients that support sampling.
```typescript
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);
});
```
## 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: