Merge pull request #698 from RishiNandha/main
Added Sampling Example to README
This commit is contained in:
commit
fb4d07f0ee
1 changed files with 120 additions and 0 deletions
57
src/examples/server/toolWithSampleServer.ts
Normal file
57
src/examples/server/toolWithSampleServer.ts
Normal file
|
|
@ -0,0 +1,57 @@
|
|||
|
||||
// Run with: npx tsx src/examples/server/toolWithSampleServer.ts
|
||||
|
||||
import { McpServer } from "../../server/mcp.js";
|
||||
import { StdioServerTransport } from "../../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);
|
||||
});
|
||||
Loading…
Reference in a new issue