|
| 1 | +/** |
| 2 | + * Type-checked examples for `mcp.ts`. |
| 3 | + * |
| 4 | + * These examples are synced into JSDoc comments via the sync-snippets script. |
| 5 | + * Each function's region markers define the code snippet that appears in the docs. |
| 6 | + * |
| 7 | + * @module |
| 8 | + */ |
| 9 | + |
| 10 | +import type { CallToolResult } from '@modelcontextprotocol/core'; |
| 11 | +import * as z from 'zod/v4'; |
| 12 | + |
| 13 | +import { McpServer } from './mcp.js'; |
| 14 | +import { StdioServerTransport } from './stdio.js'; |
| 15 | + |
| 16 | +/** |
| 17 | + * Example: Creating a new McpServer. |
| 18 | + */ |
| 19 | +function McpServer_basicUsage() { |
| 20 | + //#region McpServer_basicUsage |
| 21 | + const server = new McpServer({ |
| 22 | + name: 'my-server', |
| 23 | + version: '1.0.0' |
| 24 | + }); |
| 25 | + //#endregion McpServer_basicUsage |
| 26 | + return server; |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * Example: Registering a tool with inputSchema and outputSchema. |
| 31 | + */ |
| 32 | +function McpServer_registerTool_basic(server: McpServer) { |
| 33 | + //#region McpServer_registerTool_basic |
| 34 | + server.registerTool( |
| 35 | + 'calculate-bmi', |
| 36 | + { |
| 37 | + title: 'BMI Calculator', |
| 38 | + description: 'Calculate Body Mass Index', |
| 39 | + inputSchema: z.object({ |
| 40 | + weightKg: z.number(), |
| 41 | + heightM: z.number() |
| 42 | + }), |
| 43 | + outputSchema: z.object({ bmi: z.number() }) |
| 44 | + }, |
| 45 | + async ({ weightKg, heightM }) => { |
| 46 | + const output = { bmi: weightKg / (heightM * heightM) }; |
| 47 | + return { |
| 48 | + content: [{ type: 'text', text: JSON.stringify(output) }], |
| 49 | + structuredContent: output |
| 50 | + }; |
| 51 | + } |
| 52 | + ); |
| 53 | + //#endregion McpServer_registerTool_basic |
| 54 | +} |
| 55 | + |
| 56 | +/** |
| 57 | + * Example: Registering a static resource at a fixed URI. |
| 58 | + */ |
| 59 | +function McpServer_registerResource_static(server: McpServer) { |
| 60 | + //#region McpServer_registerResource_static |
| 61 | + server.registerResource( |
| 62 | + 'config', |
| 63 | + 'config://app', |
| 64 | + { |
| 65 | + title: 'Application Config', |
| 66 | + mimeType: 'text/plain' |
| 67 | + }, |
| 68 | + async uri => ({ |
| 69 | + contents: [{ uri: uri.href, text: 'App configuration here' }] |
| 70 | + }) |
| 71 | + ); |
| 72 | + //#endregion McpServer_registerResource_static |
| 73 | +} |
| 74 | + |
| 75 | +/** |
| 76 | + * Example: Registering a prompt with an argument schema. |
| 77 | + */ |
| 78 | +function McpServer_registerPrompt_basic(server: McpServer) { |
| 79 | + //#region McpServer_registerPrompt_basic |
| 80 | + server.registerPrompt( |
| 81 | + 'review-code', |
| 82 | + { |
| 83 | + title: 'Code Review', |
| 84 | + description: 'Review code for best practices', |
| 85 | + argsSchema: z.object({ code: z.string() }) |
| 86 | + }, |
| 87 | + ({ code }) => ({ |
| 88 | + messages: [ |
| 89 | + { |
| 90 | + role: 'user' as const, |
| 91 | + content: { |
| 92 | + type: 'text' as const, |
| 93 | + text: `Please review this code:\n\n${code}` |
| 94 | + } |
| 95 | + } |
| 96 | + ] |
| 97 | + }) |
| 98 | + ); |
| 99 | + //#endregion McpServer_registerPrompt_basic |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Example: Connecting an McpServer to a stdio transport. |
| 104 | + */ |
| 105 | +async function McpServer_connect_stdio() { |
| 106 | + //#region McpServer_connect_stdio |
| 107 | + const server = new McpServer({ name: 'my-server', version: '1.0.0' }); |
| 108 | + const transport = new StdioServerTransport(); |
| 109 | + await server.connect(transport); |
| 110 | + //#endregion McpServer_connect_stdio |
| 111 | +} |
| 112 | + |
| 113 | +/** |
| 114 | + * Example: Sending a log message to the client. |
| 115 | + */ |
| 116 | +async function McpServer_sendLoggingMessage_basic(server: McpServer) { |
| 117 | + //#region McpServer_sendLoggingMessage_basic |
| 118 | + await server.sendLoggingMessage({ |
| 119 | + level: 'info', |
| 120 | + data: 'Processing complete' |
| 121 | + }); |
| 122 | + //#endregion McpServer_sendLoggingMessage_basic |
| 123 | +} |
| 124 | + |
| 125 | +/** |
| 126 | + * Example: Logging from inside a tool handler via ctx.mcpReq.log(). |
| 127 | + */ |
| 128 | +function McpServer_registerTool_logging(server: McpServer) { |
| 129 | + //#region McpServer_registerTool_logging |
| 130 | + server.registerTool( |
| 131 | + 'fetch-data', |
| 132 | + { |
| 133 | + description: 'Fetch data from an API', |
| 134 | + inputSchema: z.object({ url: z.string() }) |
| 135 | + }, |
| 136 | + async ({ url }, ctx): Promise<CallToolResult> => { |
| 137 | + await ctx.mcpReq.log('info', `Fetching ${url}`); |
| 138 | + const res = await fetch(url); |
| 139 | + await ctx.mcpReq.log('debug', `Response status: ${res.status}`); |
| 140 | + const text = await res.text(); |
| 141 | + return { content: [{ type: 'text', text }] }; |
| 142 | + } |
| 143 | + ); |
| 144 | + //#endregion McpServer_registerTool_logging |
| 145 | +} |
0 commit comments