-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathtoolWithSampleServer.ts
More file actions
59 lines (53 loc) · 1.63 KB
/
toolWithSampleServer.ts
File metadata and controls
59 lines (53 loc) · 1.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// Run with: pnpm tsx src/toolWithSampleServer.ts
import { McpServer, StdioServerTransport } from '@modelcontextprotocol/server';
import * as z from 'zod/v4';
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: z.object({
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
});
// Since we're not passing tools param to createMessage, response.content is single content
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...');
}
try {
await main();
} catch (error) {
console.error('Server error:', error);
// eslint-disable-next-line unicorn/no-process-exit
process.exit(1);
}