mirror of
				https://github.com/Yidadaa/ChatGPT-Next-Web.git
				synced 2025-10-31 21:59:19 +08:00 
			
		
		
		
	
		
			
				
	
	
		
			56 lines
		
	
	
		
			1.3 KiB
		
	
	
	
		
			TypeScript
		
	
	
	
	
	
			
		
		
	
	
			56 lines
		
	
	
		
			1.3 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: {
 | |
|       ...Object.fromEntries(
 | |
|         Object.entries(process.env)
 | |
|           .filter(([_, v]) => v !== undefined)
 | |
|           .map(([k, v]) => [k, v as string]),
 | |
|       ),
 | |
|       ...(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());
 | |
| }
 |