mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 12:20:15 +08:00
feat: Optimize MCP configuration logic
This commit is contained in:
@@ -1,85 +1,45 @@
|
||||
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
|
||||
import { MCPClientLogger } from "./logger";
|
||||
import { McpRequestMessage } from "./types";
|
||||
import { ListToolsResponse, McpRequestMessage, ServerConfig } from "./types";
|
||||
import { z } from "zod";
|
||||
|
||||
export interface ServerConfig {
|
||||
command: string;
|
||||
args?: string[];
|
||||
env?: Record<string, string>;
|
||||
}
|
||||
|
||||
const logger = new MCPClientLogger();
|
||||
|
||||
export async function createClient(
|
||||
serverConfig: ServerConfig,
|
||||
name: string,
|
||||
id: string,
|
||||
config: ServerConfig,
|
||||
): Promise<Client> {
|
||||
logger.info(`Creating client for server ${name}`);
|
||||
logger.info(`Creating client for ${id}...`);
|
||||
|
||||
const transport = new StdioClientTransport({
|
||||
command: serverConfig.command,
|
||||
args: serverConfig.args,
|
||||
env: serverConfig.env,
|
||||
command: config.command,
|
||||
args: config.args,
|
||||
env: config.env,
|
||||
});
|
||||
|
||||
const client = new Client(
|
||||
{
|
||||
name: `nextchat-mcp-client-${name}`,
|
||||
name: `nextchat-mcp-client-${id}`,
|
||||
version: "1.0.0",
|
||||
},
|
||||
{
|
||||
capabilities: {
|
||||
// roots: {
|
||||
// listChanged: true,
|
||||
// },
|
||||
},
|
||||
capabilities: {},
|
||||
},
|
||||
);
|
||||
await client.connect(transport);
|
||||
return client;
|
||||
}
|
||||
|
||||
export interface Primitive {
|
||||
type: "resource" | "tool" | "prompt";
|
||||
value: any;
|
||||
export async function removeClient(client: Client) {
|
||||
logger.info(`Removing client...`);
|
||||
await client.close();
|
||||
}
|
||||
|
||||
/** List all resources, tools, and prompts */
|
||||
export async function listPrimitives(client: Client): Promise<Primitive[]> {
|
||||
const capabilities = client.getServerCapabilities();
|
||||
const primitives: Primitive[] = [];
|
||||
const promises = [];
|
||||
if (capabilities?.resources) {
|
||||
promises.push(
|
||||
client.listResources().then(({ resources }) => {
|
||||
resources.forEach((item) =>
|
||||
primitives.push({ type: "resource", value: item }),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (capabilities?.tools) {
|
||||
promises.push(
|
||||
client.listTools().then(({ tools }) => {
|
||||
tools.forEach((item) => primitives.push({ type: "tool", value: item }));
|
||||
}),
|
||||
);
|
||||
}
|
||||
if (capabilities?.prompts) {
|
||||
promises.push(
|
||||
client.listPrompts().then(({ prompts }) => {
|
||||
prompts.forEach((item) =>
|
||||
primitives.push({ type: "prompt", value: item }),
|
||||
);
|
||||
}),
|
||||
);
|
||||
}
|
||||
await Promise.all(promises);
|
||||
return primitives;
|
||||
export async function listTools(client: Client): Promise<ListToolsResponse> {
|
||||
return client.listTools();
|
||||
}
|
||||
|
||||
/** Execute a request */
|
||||
export async function executeRequest(
|
||||
client: Client,
|
||||
request: McpRequestMessage,
|
||||
|
Reference in New Issue
Block a user