feat: 添加 README 文档并实现 HTTP 传输模式支持
This commit is contained in:
1 parent
87878ceca1
commit
be63d49f40
3 files changed
+174
-3
No files matched your search
@@ -1,6 +1,9 @@
|
||||
#!/usr/bin/env node
|
||||
// 引入必要的模块
|
||||
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
|
||||
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
|
||||
import { StreamableHTTPServerTransport } from "@modelcontextprotocol/sdk/server/streamableHttp.js";
|
||||
import http from "http";
|
||||
import { z } from "zod";
|
||||
import { ApifoxService } from './apifox.js'
|
||||
|
||||
@@ -162,14 +165,66 @@ server.registerPrompt(
|
||||
|
||||
|
||||
|
||||
// 启动服务器,使用标准输入/输出(stdio)与客户端通信
|
||||
|
||||
// 启动服务器,使用标准输入/输出(stdio)与客户端通信
|
||||
async function main() {
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("Apifox MCP 服务器已通过 stdio 启动"); // 日志输出到 stderr
|
||||
const args = process.argv.slice(2);
|
||||
const transportArgIndex = args.indexOf('--transport') !== -1 ? args.indexOf('--transport') : args.indexOf('-t');
|
||||
const transportType = transportArgIndex !== -1 ? args[transportArgIndex + 1] : 'stdio';
|
||||
|
||||
const portArgIndex = args.indexOf('--port') !== -1 ? args.indexOf('--port') : args.indexOf('-p');
|
||||
const port = portArgIndex !== -1 ? parseInt(args[portArgIndex + 1]) : 8889;
|
||||
|
||||
|
||||
|
||||
if (transportType === 'http') {
|
||||
const transport = new StreamableHTTPServerTransport({});
|
||||
await server.connect(transport);
|
||||
|
||||
const httpServer = http.createServer(async (req, res) => {
|
||||
// 设置 CORS 头
|
||||
res.setHeader('Access-Control-Allow-Origin', '*');
|
||||
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS');
|
||||
res.setHeader('Access-Control-Allow-Headers', 'Content-Type, Authorization, mcp-session-id');
|
||||
|
||||
if (req.method === 'OPTIONS') {
|
||||
res.writeHead(204);
|
||||
res.end();
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.method === 'GET' && req.url === '/health') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ status: 'ok' }));
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
await transport.handleRequest(req, res);
|
||||
} catch (error) {
|
||||
console.error("Error handling request:", error);
|
||||
if (!res.headersSent) {
|
||||
res.writeHead(500);
|
||||
res.end("Internal Server Error");
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
httpServer.listen(port, () => {
|
||||
console.error(`Apifox MCP 服务器已通过 http 启动,监听端口 ${port}`);
|
||||
console.error(`MCP 服务 endpoint: http://localhost:${port}/sse`);
|
||||
});
|
||||
} else {
|
||||
|
||||
const transport = new StdioServerTransport();
|
||||
await server.connect(transport);
|
||||
console.error("Apifox MCP 服务器已通过 stdio 启动"); // 日志输出到 stderr
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
// 运行主函数,并捕获可能的错误
|
||||
main().catch((error) => {
|
||||
console.error("服务器运行出现致命错误:", error);
|
||||
|
||||
Reference in new issue
Block a user