import { createMCPServer } from '../src'; import { InMemoryStore } from '../src/stores'; /** * Basic MCP Server Example * * This example demonstrates how to create a simple MCP server * using the official @modelcontextprotocol/sdk under the hood. * * The server will: * - Listen on POST /mcp (standard MCP endpoint) * - Support all standard MCP clients (Inspector, Claude, etc.) * - Provide webhook subscription extensions * * Test with: * - npx @modelcontextprotocol/inspector http://localhost:3000/mcp * - curl -X POST http://localhost:3000/mcp -H "Content-Type: application/json" \ * -d '{"jsonrpc":"2.0","id":1,"method":"tools/list"}' */ const store = new InMemoryStore(); const server = createMCPServer({ name: 'example-server', version: '1.0.0', publicUrl: process.env.PUBLIC_URL || 'http://localhost:3000', port: 3000, store, // Simple tools tools: [ { name: 'echo', description: 'Echo back the input message', inputSchema: { type: 'object', properties: { message: { type: 'string', description: 'Message to echo', }, }, required: ['message'], }, handler: async (input) => { return { echo: input.message, timestamp: new Date().toISOString(), }; }, }, { name: 'add', description: 'Add two numbers', inputSchema: { type: 'object', properties: { a: { type: 'number', description: 'First number' }, b: { type: 'number', description: 'Second number' }, }, required: ['a', 'b'], }, handler: async (input) => { return { result: input.a + input.b, }; }, }, ], // Simple resources resources: [ { uri: 'example://greeting/{name}', name: 'Greeting Resource', description: 'Returns a personalized greeting', mimeType: 'text/plain', read: async (uri) => { console.log('Reading resource:', uri); const parts = uri.split('/'); const name = parts[parts.length - 1]; return { contents: `Hello, ${name}!`, }; }, list: async () => { return [ { uri: 'example://greeting/world', name: 'World Greeting', description: 'Greeting for the world', }, { uri: 'example://greeting/alice', name: 'Alice Greeting', description: 'Greeting for Alice', }, ]; }, }, ], // Optional: Simple authentication authenticate: async (req) => { // const apiKey = req.headers['x-api-key']; // if (!apiKey) { // throw new Error('API key required'); // } // In production, validate against database return { userId: 'user-123', apiKey: 'apiKey' as string, }; }, logLevel: 'info', }); // Start server server.start().then(() => { console.log('Server running!'); console.log('Try these requests:'); console.log(''); console.log('POST http://localhost:3000/mcp/tools/list'); console.log('POST http://localhost:3000/mcp/tools/call'); console.log(' Body: { "method": "tools/call", "params": { "name": "echo", "arguments": { "message": "Hello" } } }'); console.log(''); console.log('POST http://localhost:3000/mcp/resources/list'); console.log('POST http://localhost:3000/mcp/resources/read'); console.log(' Body: { "method": "resources/read", "params": { "uri": "example://greeting/world" } }'); }); // Graceful shutdown process.on('SIGTERM', async () => { console.log('Shutting down...'); await server.stop(); store.destroy(); process.exit(0); });