mirror of
https://github.com/ChatGPTNextWeb/ChatGPT-Next-Web.git
synced 2025-05-20 04:30:17 +09:00
49 lines
1.1 KiB
TypeScript
49 lines
1.1 KiB
TypeScript
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
|
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
|
import { MCPClientLogger } from "./logger";
|
|
import { ListToolsResponse, McpRequestMessage, ServerConfig } from "./types";
|
|
import { z } from "zod";
|
|
|
|
const logger = new MCPClientLogger();
|
|
|
|
export async function createClient(
|
|
id: string,
|
|
config: ServerConfig,
|
|
): Promise<Client> {
|
|
logger.info(`Creating client for ${id}...`);
|
|
|
|
const transport = new StdioClientTransport({
|
|
command: config.command,
|
|
args: config.args,
|
|
env: config.env,
|
|
});
|
|
|
|
const client = new Client(
|
|
{
|
|
name: `nextchat-mcp-client-${id}`,
|
|
version: "1.0.0",
|
|
},
|
|
{
|
|
capabilities: {},
|
|
},
|
|
);
|
|
await client.connect(transport);
|
|
return client;
|
|
}
|
|
|
|
export async function removeClient(client: Client) {
|
|
logger.info(`Removing client...`);
|
|
await client.close();
|
|
}
|
|
|
|
export async function listTools(client: Client): Promise<ListToolsResponse> {
|
|
return client.listTools();
|
|
}
|
|
|
|
export async function executeRequest(
|
|
client: Client,
|
|
request: McpRequestMessage,
|
|
) {
|
|
return client.request(request, z.any());
|
|
}
|