@aredes.me/mcp-dadosbr
Version:
Model Context Protocol server for Brazilian public data lookup (CNPJ companies and CEP postal codes) with configurable API endpoints
174 lines • 5.74 kB
JavaScript
import { KVCache } from "../core/cache.js";
import { resolveApiConfig, SERVER_VERSION } from "../config/index.js";
import { TOOL_DEFINITIONS, executeTool } from "../core/tools.js";
// CORS headers
const corsHeaders = {
"Access-Control-Allow-Origin": "*",
"Access-Control-Allow-Methods": "GET, POST, OPTIONS",
"Access-Control-Allow-Headers": "Content-Type, Accept",
"Access-Control-Max-Age": "86400",
};
function handleCORS(request) {
if (request.method === "OPTIONS") {
return new Response(null, {
status: 204,
headers: corsHeaders,
});
}
return null;
}
function handleHealthCheck() {
const healthData = {
status: "healthy",
service: "mcp-dadosbr",
version: SERVER_VERSION,
timestamp: new Date().toISOString(),
runtime: "cloudflare-workers",
};
return new Response(JSON.stringify(healthData), {
status: 200,
headers: {
"Content-Type": "application/json",
...corsHeaders,
},
});
}
export async function handleMCPRequest(request, env) {
const serverConfig = {
transport: env.MCP_TRANSPORT || "http",
httpPort: parseInt(env.MCP_HTTP_PORT || "8787"),
cacheSize: parseInt(env.MCP_CACHE_SIZE || "256"),
cacheTTL: parseInt(env.MCP_CACHE_TTL || "60000"),
apiTimeout: 8000,
};
const apiConfig = resolveApiConfig();
const cache = env.MCP_CACHE ? new KVCache(env.MCP_CACHE, serverConfig.cacheTTL) : undefined;
try {
switch (request.method) {
case "tools/list":
return {
jsonrpc: "2.0",
id: request.id,
result: {
tools: TOOL_DEFINITIONS,
},
};
case "tools/call":
const { name, arguments: args } = request.params;
try {
const result = await executeTool(name, args, apiConfig, cache);
if (result.ok) {
return {
jsonrpc: "2.0",
id: request.id,
result: {
content: [
{
type: "text",
text: JSON.stringify(result.data, null, 2),
},
],
},
};
}
else {
return {
jsonrpc: "2.0",
id: request.id,
error: {
code: -32603,
message: "Tool execution failed",
data: result.error,
},
};
}
}
catch (error) {
return {
jsonrpc: "2.0",
id: request.id,
error: {
code: -32602,
message: "Invalid parameters",
data: error.message,
},
};
}
default:
return {
jsonrpc: "2.0",
id: request.id,
error: {
code: -32601,
message: "Method not found",
data: `Unknown method: ${request.method}`,
},
};
}
}
catch (error) {
return {
jsonrpc: "2.0",
id: request.id,
error: {
code: -32603,
message: "Internal error",
data: error instanceof Error ? error.message : "Unknown error",
},
};
}
}
export async function handleMCPEndpoint(request, env) {
try {
const body = await request.text();
let mcpRequest;
try {
mcpRequest = JSON.parse(body);
}
catch (error) {
return new Response(JSON.stringify({
jsonrpc: "2.0",
id: null,
error: {
code: -32700,
message: "Parse error",
data: "Invalid JSON in request body",
},
}), {
status: 400,
headers: {
"Content-Type": "application/json",
...corsHeaders,
},
});
}
const response = await handleMCPRequest(mcpRequest, env);
return new Response(JSON.stringify(response), {
status: 200,
headers: {
"Content-Type": "application/json",
...corsHeaders,
},
});
}
catch (error) {
console.error("Error handling MCP request:", error);
return new Response(JSON.stringify({
jsonrpc: "2.0",
id: null,
error: {
code: -32603,
message: "Internal error",
data: error instanceof Error ? error.message : "Unknown error",
},
}), {
status: 500,
headers: {
"Content-Type": "application/json",
...corsHeaders,
},
});
}
}
export { corsHeaders, handleCORS, handleHealthCheck };
//# sourceMappingURL=cloudflare.js.map