feat: add hello-world MCP server scaffold
Minimal dependency-free Node.js MCP server (JSON-RPC 2.0 over stdio). Exposes one tool: say_hello. Handles initialize, tools/list, tools/call, and SIGTERM cleanly. No npm packages required — uses only readline. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
69
plugins/hello-world/bin/mcp-server.js
Executable file
69
plugins/hello-world/bin/mcp-server.js
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env node
|
||||
'use strict';
|
||||
|
||||
// Minimal MCP server — JSON-RPC 2.0 over stdio, no external dependencies.
|
||||
// Exposes one tool: `say_hello`. Replace with your actual tools.
|
||||
|
||||
const readline = require('readline');
|
||||
|
||||
const rl = readline.createInterface({ input: process.stdin, terminal: false });
|
||||
|
||||
function send(obj) {
|
||||
process.stdout.write(JSON.stringify(obj) + '\n');
|
||||
}
|
||||
|
||||
const TOOLS = [
|
||||
{
|
||||
name: 'say_hello',
|
||||
description: 'Say hello from the holocron marketplace hello-world plugin.',
|
||||
inputSchema: {
|
||||
type: 'object',
|
||||
properties: {
|
||||
name: { type: 'string', description: 'Name to greet (optional)' }
|
||||
}
|
||||
}
|
||||
}
|
||||
];
|
||||
|
||||
rl.on('line', (line) => {
|
||||
let msg;
|
||||
try { msg = JSON.parse(line); } catch { return; }
|
||||
|
||||
const { method, id, params } = msg;
|
||||
|
||||
if (method === 'initialize') {
|
||||
send({
|
||||
jsonrpc: '2.0', id,
|
||||
result: {
|
||||
protocolVersion: '2024-11-05',
|
||||
capabilities: { tools: {} },
|
||||
serverInfo: { name: 'hello-world', version: '1.0.0' }
|
||||
}
|
||||
});
|
||||
|
||||
} else if (method === 'notifications/initialized') {
|
||||
// no response required
|
||||
|
||||
} else if (method === 'tools/list') {
|
||||
send({ jsonrpc: '2.0', id, result: { tools: TOOLS } });
|
||||
|
||||
} else if (method === 'tools/call') {
|
||||
const toolName = params?.name;
|
||||
const args = params?.arguments ?? {};
|
||||
|
||||
if (toolName === 'say_hello') {
|
||||
const greeting = args.name
|
||||
? `Hello, ${args.name}! The holocron marketplace hello-world plugin is working.`
|
||||
: 'Hello from the holocron marketplace hello-world plugin!';
|
||||
send({ jsonrpc: '2.0', id, result: { content: [{ type: 'text', text: greeting }] } });
|
||||
} else {
|
||||
send({ jsonrpc: '2.0', id, error: { code: -32601, message: `Unknown tool: ${toolName}` } });
|
||||
}
|
||||
|
||||
} else if (id !== undefined) {
|
||||
send({ jsonrpc: '2.0', id, error: { code: -32601, message: 'Method not found' } });
|
||||
}
|
||||
});
|
||||
|
||||
process.on('SIGINT', () => process.exit(0));
|
||||
process.on('SIGTERM', () => process.exit(0));
|
||||
Reference in New Issue
Block a user