mcpServerOutputSchema.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #!/usr/bin/env node
  2. "use strict";
  3. /**
  4. * Example MCP server using the high-level McpServer API with outputSchema
  5. * This demonstrates how to easily create tools with structured output
  6. */
  7. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  8. if (k2 === undefined) k2 = k;
  9. var desc = Object.getOwnPropertyDescriptor(m, k);
  10. if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
  11. desc = { enumerable: true, get: function() { return m[k]; } };
  12. }
  13. Object.defineProperty(o, k2, desc);
  14. }) : (function(o, m, k, k2) {
  15. if (k2 === undefined) k2 = k;
  16. o[k2] = m[k];
  17. }));
  18. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  19. Object.defineProperty(o, "default", { enumerable: true, value: v });
  20. }) : function(o, v) {
  21. o["default"] = v;
  22. });
  23. var __importStar = (this && this.__importStar) || function (mod) {
  24. if (mod && mod.__esModule) return mod;
  25. var result = {};
  26. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  27. __setModuleDefault(result, mod);
  28. return result;
  29. };
  30. Object.defineProperty(exports, "__esModule", { value: true });
  31. const mcp_js_1 = require("../../server/mcp.js");
  32. const stdio_js_1 = require("../../server/stdio.js");
  33. const z = __importStar(require("zod/v4"));
  34. const server = new mcp_js_1.McpServer({
  35. name: 'mcp-output-schema-high-level-example',
  36. version: '1.0.0'
  37. });
  38. // Define a tool with structured output - Weather data
  39. server.registerTool('get_weather', {
  40. description: 'Get weather information for a city',
  41. inputSchema: {
  42. city: z.string().describe('City name'),
  43. country: z.string().describe('Country code (e.g., US, UK)')
  44. },
  45. outputSchema: {
  46. temperature: z.object({
  47. celsius: z.number(),
  48. fahrenheit: z.number()
  49. }),
  50. conditions: z.enum(['sunny', 'cloudy', 'rainy', 'stormy', 'snowy']),
  51. humidity: z.number().min(0).max(100),
  52. wind: z.object({
  53. speed_kmh: z.number(),
  54. direction: z.string()
  55. })
  56. }
  57. }, async ({ city, country }) => {
  58. // Parameters are available but not used in this example
  59. void city;
  60. void country;
  61. // Simulate weather API call
  62. const temp_c = Math.round((Math.random() * 35 - 5) * 10) / 10;
  63. const conditions = ['sunny', 'cloudy', 'rainy', 'stormy', 'snowy'][Math.floor(Math.random() * 5)];
  64. const structuredContent = {
  65. temperature: {
  66. celsius: temp_c,
  67. fahrenheit: Math.round(((temp_c * 9) / 5 + 32) * 10) / 10
  68. },
  69. conditions,
  70. humidity: Math.round(Math.random() * 100),
  71. wind: {
  72. speed_kmh: Math.round(Math.random() * 50),
  73. direction: ['N', 'NE', 'E', 'SE', 'S', 'SW', 'W', 'NW'][Math.floor(Math.random() * 8)]
  74. }
  75. };
  76. return {
  77. content: [
  78. {
  79. type: 'text',
  80. text: JSON.stringify(structuredContent, null, 2)
  81. }
  82. ],
  83. structuredContent
  84. };
  85. });
  86. async function main() {
  87. const transport = new stdio_js_1.StdioServerTransport();
  88. await server.connect(transport);
  89. console.error('High-level Output Schema Example Server running on stdio');
  90. }
  91. main().catch(error => {
  92. console.error('Server error:', error);
  93. process.exit(1);
  94. });
  95. //# sourceMappingURL=mcpServerOutputSchema.js.map