From 950a9304655f99b5d307cd0304d4549ad8602564 Mon Sep 17 00:00:00 2001 From: Claude AI Date: Wed, 25 Mar 2026 00:34:56 +0000 Subject: [PATCH] =?UTF-8?q?feat:=20=E6=B7=BB=E5=8A=A0=20MCP=20=E5=81=A5?= =?UTF-8?q?=E5=BA=B7=E6=A3=80=E6=9F=A5=20API=20=E8=B7=AF=E7=94=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 新增 /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 --- .../app/api/mcp/health/[mcpType]/route.ts | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 frontend-v2/app/api/mcp/health/[mcpType]/route.ts diff --git a/frontend-v2/app/api/mcp/health/[mcpType]/route.ts b/frontend-v2/app/api/mcp/health/[mcpType]/route.ts new file mode 100644 index 0000000..051d352 --- /dev/null +++ b/frontend-v2/app/api/mcp/health/[mcpType]/route.ts @@ -0,0 +1,66 @@ +import { NextRequest } from 'next/server'; + +// MCP 服务器配置 +const MCP_SERVERS: Record = { + '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' } } + ); + } +}