Files
cc-switch/src/lib/schemas/mcp.ts
T
Jason bfc27349b3 feat(mcp): add SSE (Server-Sent Events) transport type support
Add comprehensive support for SSE transport type to MCP server configuration,
enabling real-time streaming connections alongside existing stdio and http types.

Backend Changes:
- Add SSE type validation in mcp.rs validate_server_spec()
- Extend Codex TOML import/export to handle SSE servers
- Update claude_mcp.rs legacy API for backward compatibility
- Unify http/sse handling in json_server_to_toml_table()

Frontend Changes:
- Extend McpServerSpec type definition to include "sse"
- Add SSE radio button to configuration wizard UI
- Update wizard form logic to handle SSE url and headers
- Add SSE validation in McpFormModal submission

Validation & Error Handling:
- Add SSE support in useMcpValidation hook (TOML/JSON)
- Extend tomlUtils normalizeServerConfig for SSE parsing
- Update Zod schemas (common.ts, mcp.ts) with SSE enum
- Add SSE error message mapping in errorUtils

Internationalization:
- Add "typeSse" translations (zh: "sse", en: "sse")

Tests:
- Add SSE validation test cases in useMcpValidation.test.tsx

SSE Configuration Format:
{
  "type": "sse",
  "url": "https://api.example.com/sse",
  "headers": { "Authorization": "Bearer token" }
}
2025-11-16 16:15:17 +08:00

43 lines
1.3 KiB
TypeScript

import { z } from "zod";
const mcpServerSpecSchema = z
.object({
type: z.enum(["stdio", "http", "sse"]).optional(),
command: z.string().trim().optional(),
args: z.array(z.string()).optional(),
env: z.record(z.string(), z.string()).optional(),
cwd: z.string().optional(),
url: z.string().trim().url("请输入有效的 URL").optional(),
headers: z.record(z.string(), z.string()).optional(),
})
.superRefine((server, ctx) => {
const type = server.type ?? "stdio";
if (type === "stdio" && !server.command?.trim()) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: "stdio 类型需填写 command",
path: ["command"],
});
}
if ((type === "http" || type === "sse") && !server.url?.trim()) {
ctx.addIssue({
code: z.ZodIssueCode.custom,
message: `${type} 类型需填写 url`,
path: ["url"],
});
}
});
export const mcpServerSchema = z.object({
id: z.string().min(1, "请输入服务器 ID"),
name: z.string().optional(),
description: z.string().optional(),
tags: z.array(z.string()).optional(),
homepage: z.string().url().optional(),
docs: z.string().url().optional(),
enabled: z.boolean().optional(),
server: mcpServerSpecSchema,
});
export type McpServerFormData = z.infer<typeof mcpServerSchema>;