feat: 添加 MCP 健康检查 API 路由

新增 /api/mcp/health/[mcpType] 动态路由,用于检查各 MCP 服务器的健康状态。

- 支持 novel-translator、novel-platform-user、novel-platform-admin、template-241-mcp-app
- 返回健康状态、延迟、URL 和认证类型
- 405 Method Not Allowed 视为健康(MCP 使用 POST)

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Claude AI
2026-03-25 00:34:56 +00:00
parent 34e0a4d8ba
commit 950a930465

View File

@@ -0,0 +1,66 @@
import { NextRequest } from 'next/server';
// MCP 服务器配置
const MCP_SERVERS: Record<string, { url: string; authType: string }> = {
'novel-translator': {
url: 'https://d8d-ai-vscode-8080-223-236-template-6-group.dev.d8d.fun/mcp',
authType: 'none',
},
'novel-platform-user': {
url: 'https://d8d-ai-vscode-8080-223-238-template-6-group.dev.d8d.fun/mcp/',
authType: 'jwt',
},
'novel-platform-admin': {
url: 'https://d8d-ai-vscode-8080-223-238-template-6-group.dev.d8d.fun/admin-mcp/',
authType: 'jwt',
},
'template-241-mcp-app': {
url: 'https://d8d-ai-vscode-8080-223-241-template-6-group.dev.d8d.fun/mcp',
authType: 'none',
},
};
export async function GET(
request: NextRequest,
{ params }: { params: { mcpType: string } }
) {
const { mcpType } = params;
const config = MCP_SERVERS[mcpType];
if (!config) {
return new Response(
JSON.stringify({ healthy: false, error: '未知的 MCP 类型' }),
{ status: 404, headers: { 'Content-Type': 'application/json' } }
);
}
try {
const startTime = Date.now();
const response = await fetch(config.url, {
method: 'GET',
headers: { 'Accept': 'application/json' },
});
const latency = Date.now() - startTime;
const healthy = response.ok || response.status === 405; // 405 Method Not Allowed 也算健康MCP 用 POST
return new Response(
JSON.stringify({
healthy,
status: response.ok ? 'healthy' : 'unhealthy',
latency,
url: config.url,
authType: config.authType,
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
} catch (error) {
return new Response(
JSON.stringify({
healthy: false,
error: error instanceof Error ? error.message : String(error),
}),
{ status: 200, headers: { 'Content-Type': 'application/json' } }
);
}
}