@coincap/coincap-mcp-server
Version:
CoinCap.io MCP server serving the coincap rest api via mcp
95 lines (94 loc) • 4.18 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __exportStar = (this && this.__exportStar) || function(m, exports) {
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const index_js_1 = require("@modelcontextprotocol/sdk/server/index.js");
const stdio_js_1 = require("@modelcontextprotocol/sdk/server/stdio.js");
const types_js_1 = require("@modelcontextprotocol/sdk/types.js");
const express_1 = __importDefault(require("express"));
const cors_1 = __importDefault(require("cors"));
const mcpRouter_js_1 = require("./mcpRouter.js");
const dynamicMcpTools_js_1 = require("./dynamicMcpTools.js");
class DynamicMCPServer {
constructor() {
this.server = new index_js_1.Server({
name: 'cryptocurrency-mcp-server',
version: '0.2.0'
}, {
capabilities: { tools: {} }
});
}
async setup() {
await (0, dynamicMcpTools_js_1.loadSwaggerEndpoints)();
this.server.setRequestHandler(types_js_1.ListToolsRequestSchema, async () => ({
tools: Object.values(dynamicMcpTools_js_1.endpointMap).map(def => ({
name: def.toolName,
description: def.description,
inputSchema: {
type: 'object',
properties: Object.fromEntries([...def.pathParams, ...def.queryParams].map(param => [
param.name,
{
type: param.type,
description: param.description,
...(param.enum ? { enum: param.enum } : {}),
...(param.example ? { example: param.example } : {})
}
])),
required: [
...def.pathParams.filter(p => p.required).map(p => p.name),
...def.queryParams.filter(p => p.required).map(p => p.name)
]
}
}))
}));
this.server.setRequestHandler(types_js_1.CallToolRequestSchema, async (req) => {
const { name, arguments: args } = req.params;
return await (0, mcpRouter_js_1.callToolFromStdio)(name, args);
});
}
async run() {
await this.setup();
const portArg = process.argv.find(arg => arg.startsWith('--port='));
if (portArg) {
const port = parseInt(portArg.split('=')[1]);
await this.startHttpServer(port);
}
else {
const transport = new stdio_js_1.StdioServerTransport();
await this.server.connect(transport);
console.error(`🚀 MCP server ready via stdio`);
}
}
async startHttpServer(port) {
const app = (0, express_1.default)();
app.use((0, cors_1.default)());
app.use(express_1.default.json()); // Handles application/json (standard JSON POST)
app.use(express_1.default.text({ type: 'text/plain' })); // Optional: for NDJSON or raw text, if you want to keep it
app.use('/mcp', (0, mcpRouter_js_1.createMCPRouter)());
app.listen(port, '127.0.0.1', () => {
const url = `http://127.0.0.1:${port}/mcp`;
console.error(`🚀 Streamble http MCP server running at ${url}`);
});
}
}
if (require.main === module) {
new DynamicMCPServer().run().catch(console.error);
}
__exportStar(require("./mcpRouter"), exports);