Add StreamableHttp support

This commit is contained in:
YISH
2025-05-20 00:02:30 +08:00
parent 62d32f317d
commit 2f20905544
4 changed files with 485 additions and 22 deletions

View File

@@ -1,11 +1,6 @@
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
import { MCPClientLogger } from "./logger";
import {
ListToolsResponse,
McpRequestMessage,
ServerConfig,
isServerSseConfig,
} from "./types";
import { ListToolsResponse, McpRequestMessage, ServerConfig } from "./types";
import { z } from "zod";
const logger = new MCPClientLogger();
@@ -18,11 +13,16 @@ export async function createClient(
let transport;
if (isServerSseConfig(config)) {
if (config.type === "sse") {
const { SSEClientTransport } = await import(
"@modelcontextprotocol/sdk/client/sse.js"
);
transport = new SSEClientTransport(new URL(config.url));
} else if (config.type === "streamable") {
const { StreamableHTTPClientTransport } = await import(
"@modelcontextprotocol/sdk/client/streamableHttp.js"
);
transport = new StreamableHTTPClientTransport(new URL(config.url));
} else {
if (EXPORT_MODE) {
throw new Error(

View File

@@ -116,10 +116,15 @@ export interface ServerStatusResponse {
export const isServerSseConfig = (c?: ServerConfig): c is ServerSseConfig =>
c !== null && typeof c === "object" && c.type === "sse";
export const isStreamableSseConfig = (c?: ServerConfig): c is ServerSseConfig =>
c !== null && typeof c === "object" && c.type === "streamable";
export const isServerStdioConfig = (c?: ServerConfig): c is ServerStdioConfig =>
c !== null && typeof c === "object" && (!c.type || c.type === "stdio");
export type ServerConfig = ServerStdioConfig | ServerSseConfig;
export type ServerConfig =
| ServerStdioConfig
| ServerSseConfig
| ServerSteamableConfig;
export interface ServerStdioConfig {
type?: "stdio";
@@ -136,6 +141,13 @@ export interface ServerSseConfig {
status?: "active" | "paused" | "error";
}
export interface ServerSteamableConfig {
type: "streamable";
url: string;
headers?: Record<string, string>;
status?: "active" | "paused" | "error";
}
export interface McpConfigData {
enableMcp?: boolean;
// MCP Server 的配置