mcp-quiz-server
Version:
🧠AI-Powered Quiz Management via Model Context Protocol (MCP) - Create, manage, and take quizzes directly from VS Code, Claude, and other AI agents.
82 lines (81 loc) • 3.35 kB
JavaScript
;
/**
* @moduleName: MCP Server - STDIO Transport Layer
* @version: 1.0.0
* @since: 2025-07-21
* @lastUpdated: 2025-07-27
* @projectSummary: Pure STDIO MCP server for VS Code integration using JSON-RPC 2.0 over standard input/output streams
* @techStack: TypeScript, Node.js STDIO, JSON-RPC 2.0, MCP Protocol
* @dependency: process (Node.js built-in)
* @interModuleDependency: ./mcp-handler (shared business logic)
* @requirementsTraceability:
* {@link Requirements.REQ_MCP_007} (VS Code Integration)
* {@link Requirements.REQ_MCP_008} (STDIO Transport)
* {@link Requirements.REQ_MCP_009} (Silent Operation)
* @briefDescription: STDIO transport layer for MCP protocol, designed for VS Code extension integration. Processes JSON-RPC messages from STDIN and responds via STDOUT with no console logging to maintain clean communication channel
* @methods: STDIN data handler, JSON-RPC message processing, STDOUT response writer
* @contributors: Jorge Sequeira
* @examples:
* - Usage in VS Code: node dist/mcp-server.js (configured in .vscode/mcp.json)
* - Input: {"jsonrpc":"2.0","method":"initialize","id":1}
* - Output: {"jsonrpc":"2.0","id":1,"result":{"...capabilities..."}}
* @vulnerabilitiesAssessment:
* - No network exposure (STDIO only)
* - JSON parsing error handling prevents crashes
* - Delegates security to mcp-handler module
* - Process isolation through VS Code sandbox
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Pure STDIO MCP Server for VS Code Integration
* This server communicates via STDIN/STDOUT using JSON-RPC 2.0
*/
const mcp_handler_1 = require("./mcp/handlers/mcp-handler");
// Handle STDIO communication
process.stdin.setEncoding('utf8');
let inputBuffer = '';
process.stdin.on('data', async (chunk) => {
inputBuffer += chunk;
// Process complete JSON-RPC messages
const lines = inputBuffer.split('\n');
inputBuffer = lines.pop() || ''; // Keep incomplete line in buffer
for (const line of lines) {
if (line.trim()) {
try {
const request = JSON.parse(line.trim());
const response = await mcp_handler_1.simplifiedModularHandler.handleRequest(request);
// Send response via STDOUT
process.stdout.write(JSON.stringify(response) + '\n');
}
catch (error) {
// Send error response
const errorResponse = {
jsonrpc: '2.0',
id: null,
error: {
code: -32700,
message: 'Parse error',
data: error instanceof Error ? error.message : String(error),
},
};
process.stdout.write(JSON.stringify(errorResponse) + '\n');
}
}
}
});
process.stdin.on('end', () => {
process.exit(0);
});
// Handle process termination
process.on('SIGINT', () => {
process.exit(0);
});
process.on('SIGTERM', () => {
process.exit(0);
});
// Optional: Send server info on startup (for debugging)
if (process.env.NODE_ENV === 'development') {
console.error('MCP Quiz Server (STDIO mode) started');
console.error('Server ready for JSON-RPC 2.0 communication via STDIN/STDOUT');
}