mcpServerOutputSchema.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #!/usr/bin/env node
  2. /**
  3. * Example MCP server using the high-level McpServer API with outputSchema
  4. * This demonstrates how to easily create tools with structured output
  5. */
  6. import { McpServer } from '../../server/mcp.js';
  7. import { StdioServerTransport } from '../../server/stdio.js';
  8. import * as z from 'zod/v4';
  9. const server = new McpServer({
  10. name: 'mcp-output-schema-high-level-example',
  11. version: '1.0.0'
  12. });
  13. // Define a tool with structured output - Weather data
  14. server.registerTool('get_weather', {
  15. description: 'Get weather information for a city',
  16. inputSchema: {
  17. city: z.string().describe('City name'),
  18. country: z.string().describe('Country code (e.g., US, UK)')
  19. },
  20. outputSchema: {
  21. temperature: z.object({
  22. celsius: z.number(),
  23. fahrenheit: z.number()
  24. }),
  25. conditions: z.enum(['sunny', 'cloudy', 'rainy', 'stormy', 'snowy']),
  26. humidity: z.number().min(0).max(100),
  27. wind: z.object({
  28. speed_kmh: z.number(),
  29. direction: z.string()
  30. })
  31. }
  32. }, async ({ city, country }) => {
  33. // Parameters are available but not used in this example
  34. void city;
  35. void country;
  36. // Simulate weather API call
  37. const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
  38. const conditions = ['sunny', 'cloudy', 'rainy', 'stormy', 'snowy'][Math.floor(Math.random() * 5)];
  39. const structuredContent = {
  40. temperature: {
  41. celsius: temp_c,
  42. fahrenheit: Math.round(((temp_c * 9) / 5 + 32) * 10) / 10
  43. },
  44. conditions,
  45. humidity: Math.round(Math.random() * 100),
  46. wind: {
  47. speed_kmh: Math.round(Math.random() * 50),
  48. direction: ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][Math.floor(Math.random() * 8)]
  49. }
  50. };
  51. return {
  52. content: [
  53. {
  54. type: 'text',
  55. text: JSON.stringify(structuredContent, null, 2)
  56. }
  57. ],
  58. structuredContent
  59. };
  60. });
  61. async function main() {
  62. const transport = new StdioServerTransport();
  63. await server.connect(transport);
  64. console.error('High-level Output Schema Example Server running on stdio');
  65. }
  66. main().catch(error => {
  67. console.error('Server error:', error);
  68. process.exit(1);
  69. });
  70. //# sourceMappingURL=mcpServerOutputSchema.js.map