toolWithSampleServer.js 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. // Run with: npx tsx src/examples/server/toolWithSampleServer.ts
  2. import { McpServer } from '../../server/mcp.js';
  3. import { StdioServerTransport } from '../../server/stdio.js';
  4. import * as z from 'zod/v4';
  5. const mcpServer = new McpServer({
  6. name: 'tools-with-sample-server',
  7. version: '1.0.0'
  8. });
  9. // Tool that uses LLM sampling to summarize any text
  10. mcpServer.registerTool('summarize', {
  11. description: 'Summarize any text using an LLM',
  12. inputSchema: {
  13. text: z.string().describe('Text to summarize')
  14. }
  15. }, async ({ text }) => {
  16. // Call the LLM through MCP sampling
  17. const response = await mcpServer.server.createMessage({
  18. messages: [
  19. {
  20. role: 'user',
  21. content: {
  22. type: 'text',
  23. text: `Please summarize the following text concisely:\n\n${text}`
  24. }
  25. }
  26. ],
  27. maxTokens: 500
  28. });
  29. // Since we're not passing tools param to createMessage, response.content is single content
  30. return {
  31. content: [
  32. {
  33. type: 'text',
  34. text: response.content.type === 'text' ? response.content.text : 'Unable to generate summary'
  35. }
  36. ]
  37. };
  38. });
  39. async function main() {
  40. const transport = new StdioServerTransport();
  41. await mcpServer.connect(transport);
  42. console.log('MCP server is running...');
  43. }
  44. main().catch(error => {
  45. console.error('Server error:', error);
  46. process.exit(1);
  47. });
  48. //# sourceMappingURL=toolWithSampleServer.js.map