Skip to main content
Glama

MCP Server Scaffold

by vjouenne76
index.ts3.42 kB
#!/usr/bin/env node import { Server } from '@modelcontextprotocol/sdk/server/index.js'; import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'; import { CallToolRequestSchema, ListToolsRequestSchema, Tool, } from '@modelcontextprotocol/sdk/types.js'; /** * MCP Server Scaffold * * This is a basic MCP server implementation that provides: * - A sample tool for demonstration * - Proper error handling * - Standard MCP protocol compliance */ class MCPServerScaffold { private server: Server; constructor() { this.server = new Server( { name: 'mcp-server-scaffold', version: '1.0.0', }, { capabilities: { tools: {}, }, } ); this.setupToolHandlers(); this.setupErrorHandling(); } private setupToolHandlers(): void { // List available tools this.server.setRequestHandler(ListToolsRequestSchema, async () => { return { tools: [ { name: 'echo', description: 'Echo back the provided message', inputSchema: { type: 'object', properties: { message: { type: 'string', description: 'The message to echo back', }, }, required: ['message'], }, } as Tool, { name: 'get_current_time', description: 'Get the current date and time', inputSchema: { type: 'object', properties: {}, }, } as Tool, ], }; }); // Handle tool calls this.server.setRequestHandler(CallToolRequestSchema, async (request) => { const { name, arguments: args } = request.params; try { switch (name) { case 'echo': return { content: [ { type: 'text', text: `Echo: ${args.message}`, }, ], }; case 'get_current_time': const now = new Date(); return { content: [ { type: 'text', text: `Current time: ${now.toISOString()}`, }, ], }; default: throw new Error(`Unknown tool: ${name}`); } } catch (error) { const errorMessage = error instanceof Error ? error.message : 'Unknown error'; return { content: [ { type: 'text', text: `Error: ${errorMessage}`, }, ], isError: true, }; } }); } private setupErrorHandling(): void { this.server.onerror = (error) => { console.error('[MCP Error]', error); }; process.on('SIGINT', async () => { await this.server.close(); process.exit(0); }); } async run(): Promise<void> { const transport = new StdioServerTransport(); await this.server.connect(transport); // Server is now running and will handle requests via stdio console.error('MCP Server Scaffold started successfully'); } } // Start the server const server = new MCPServerScaffold(); server.run().catch((error) => { console.error('Failed to start MCP server:', error); process.exit(1); });

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/vjouenne76/mcp-server-scaffold'

If you have feedback or need assistance with the MCP directory API, please join our Discord server