File size: 1,667 Bytes
7a1ad33 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | /**
* @license
* Copyright 2026 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import {
ListToolsRequestSchema,
CallToolRequestSchema,
} from '@modelcontextprotocol/sdk/types.js';
import fs from 'fs';
const configPath = process.argv[2];
if (!configPath) {
console.error('Usage: node template.mjs <config-path>');
process.exit(1);
}
const config = JSON.parse(fs.readFileSync(configPath, 'utf-8'));
const server = new Server(
{
name: config.name,
version: config.version || '1.0.0',
},
{
capabilities: {
tools: {},
},
},
);
// Add tools handler
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: (config.tools || []).map((tool) => ({
name: tool.name,
description: tool.description,
inputSchema: tool.inputSchema || { type: 'object', properties: {} },
})),
};
});
// Add call handler
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const toolName = request.params.name;
const tool = (config.tools || []).find((t) => t.name === toolName);
if (!tool) {
return {
content: [
{
type: 'text',
text: `Error: Tool ${toolName} not found`,
},
],
isError: true,
};
}
return tool.response;
});
const transport = new StdioServerTransport();
await server.connect(transport);
// server.connect resolves when transport connects, but listening continues
console.error(`Test MCP Server '${config.name}' connected and listening.`);
|