|
1 | 1 | #!/usr/bin/env node
|
2 | 2 | /**
|
3 |
| - * @fileoverview MCP server entry point that sets up and starts the server |
| 3 | + * @fileoverview Entry point that supports both MCP server and CLI modes |
4 | 4 | * @module index
|
5 | 5 | */
|
6 | 6 |
|
7 | 7 | import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
8 | 8 | import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
|
9 | 9 | import { CallToolRequestSchema, ListToolsRequestSchema } from '@modelcontextprotocol/sdk/types.js';
|
10 |
| -import { z } from 'zod'; |
11 | 10 | import { zodToJsonSchema } from 'zod-to-json-schema';
|
| 11 | +import { createCLI } from './cli.js'; |
12 | 12 | import { ExampleToolSchema, exampleTool } from './tools/example.js';
|
13 | 13 |
|
14 | 14 | /**
|
15 |
| - * Create the MCP server instance with configured capabilities |
| 15 | + * Determine if we're running in CLI mode |
| 16 | + * CLI mode is detected when command line arguments are provided beyond node and script name |
16 | 17 | */
|
17 |
| -const server = new Server( |
18 |
| - { |
19 |
| - name: 'mcp-template', |
20 |
| - version: '0.1.0', |
21 |
| - }, |
22 |
| - { |
23 |
| - capabilities: { |
24 |
| - tools: {}, |
25 |
| - }, |
26 |
| - }, |
27 |
| -); |
| 18 | +const isCliMode = process.argv.length > 2; |
28 | 19 |
|
29 | 20 | /**
|
30 |
| - * Register handler for listing available tools |
31 |
| - * @returns List of available tools with their schemas |
| 21 | + * Main entry point that handles both MCP and CLI modes |
32 | 22 | */
|
33 |
| -server.setRequestHandler(ListToolsRequestSchema, async () => { |
34 |
| - return { |
35 |
| - tools: [ |
36 |
| - { |
37 |
| - name: 'example_tool', |
38 |
| - description: 'An example tool that echoes back the input', |
39 |
| - inputSchema: zodToJsonSchema(ExampleToolSchema), |
40 |
| - }, |
41 |
| - ], |
42 |
| - }; |
43 |
| -}); |
| 23 | +async function main() { |
| 24 | + if (isCliMode) { |
| 25 | + // CLI Mode: Run as command-line tool |
| 26 | + const program = createCLI(); |
| 27 | + await program.parseAsync(process.argv); |
| 28 | + } else { |
| 29 | + // MCP Mode: Run as MCP server |
| 30 | + await startMcpServer(); |
| 31 | + } |
| 32 | +} |
44 | 33 |
|
45 | 34 | /**
|
46 |
| - * Register handler for executing tool calls |
47 |
| - * @param request - The tool call request containing tool name and arguments |
48 |
| - * @returns Tool execution result |
| 35 | + * Start the MCP server with all configured tools and handlers |
49 | 36 | */
|
50 |
| -server.setRequestHandler(CallToolRequestSchema, async (request) => { |
51 |
| - const { name, arguments: args } = request.params; |
| 37 | +async function startMcpServer() { |
| 38 | + /** |
| 39 | + * Create the MCP server instance with configured capabilities |
| 40 | + */ |
| 41 | + const server = new Server( |
| 42 | + { |
| 43 | + name: 'mcp-template', |
| 44 | + version: '0.1.0', |
| 45 | + }, |
| 46 | + { |
| 47 | + capabilities: { |
| 48 | + tools: {}, |
| 49 | + }, |
| 50 | + }, |
| 51 | + ); |
52 | 52 |
|
53 |
| - switch (name) { |
54 |
| - case 'example_tool': |
55 |
| - return await exampleTool(args); |
56 |
| - default: |
57 |
| - throw new Error(`Unknown tool: ${name}`); |
58 |
| - } |
59 |
| -}); |
| 53 | + /** |
| 54 | + * Register handler for listing available tools |
| 55 | + * @returns List of available tools with their schemas |
| 56 | + */ |
| 57 | + server.setRequestHandler(ListToolsRequestSchema, async () => { |
| 58 | + return { |
| 59 | + tools: [ |
| 60 | + { |
| 61 | + name: 'example_tool', |
| 62 | + description: 'An example tool that echoes back the input', |
| 63 | + inputSchema: zodToJsonSchema(ExampleToolSchema), |
| 64 | + }, |
| 65 | + ], |
| 66 | + }; |
| 67 | + }); |
60 | 68 |
|
61 |
| -/** |
62 |
| - * Start the MCP server using stdio transport |
63 |
| - */ |
64 |
| -const transport = new StdioServerTransport(); |
65 |
| -await server.connect(transport); |
| 69 | + /** |
| 70 | + * Register handler for executing tool calls |
| 71 | + * @param request - The tool call request containing tool name and arguments |
| 72 | + * @returns Tool execution result |
| 73 | + */ |
| 74 | + server.setRequestHandler(CallToolRequestSchema, async (request) => { |
| 75 | + const { name, arguments: args } = request.params; |
66 | 76 |
|
67 |
| -/** |
68 |
| - * Handle graceful shutdown on SIGINT (Ctrl+C) |
69 |
| - */ |
70 |
| -process.on('SIGINT', async () => { |
71 |
| - await server.close(); |
72 |
| - process.exit(0); |
73 |
| -}); |
| 77 | + switch (name) { |
| 78 | + case 'example_tool': |
| 79 | + return await exampleTool(args); |
| 80 | + default: |
| 81 | + throw new Error(`Unknown tool: ${name}`); |
| 82 | + } |
| 83 | + }); |
| 84 | + |
| 85 | + /** |
| 86 | + * Start the MCP server using stdio transport |
| 87 | + */ |
| 88 | + const transport = new StdioServerTransport(); |
| 89 | + await server.connect(transport); |
| 90 | + |
| 91 | + /** |
| 92 | + * Handle graceful shutdown on SIGINT (Ctrl+C) |
| 93 | + */ |
| 94 | + process.on('SIGINT', async () => { |
| 95 | + await server.close(); |
| 96 | + process.exit(0); |
| 97 | + }); |
| 98 | +} |
| 99 | + |
| 100 | +// Start the application |
| 101 | +main().catch(console.error); |
0 commit comments