mirror of
https://github.com/Yidadaa/ChatGPT-Next-Web.git
synced 2025-08-08 07:10:16 +08:00
feat: MCP message type
This commit is contained in:
@@ -3,8 +3,9 @@
|
||||
import { createClient, executeRequest } from "./client";
|
||||
import { MCPClientLogger } from "./logger";
|
||||
import conf from "./mcp_config.json";
|
||||
import { McpRequestMessage } from "./types";
|
||||
|
||||
const logger = new MCPClientLogger("MCP Server");
|
||||
const logger = new MCPClientLogger("MCP Actions");
|
||||
|
||||
// Use Map to store all clients
|
||||
const clientsMap = new Map<string, any>();
|
||||
@@ -51,7 +52,10 @@ export async function initializeMcpClients() {
|
||||
}
|
||||
|
||||
// Execute MCP request
|
||||
export async function executeMcpAction(clientId: string, request: any) {
|
||||
export async function executeMcpAction(
|
||||
clientId: string,
|
||||
request: McpRequestMessage,
|
||||
) {
|
||||
try {
|
||||
// Find the corresponding client
|
||||
const client = clientsMap.get(clientId);
|
||||
@@ -61,6 +65,7 @@ export async function executeMcpAction(clientId: string, request: any) {
|
||||
}
|
||||
|
||||
logger.info(`Executing MCP request for ${clientId}`);
|
||||
|
||||
// Execute request and return result
|
||||
return await executeRequest(client, request);
|
||||
} catch (error) {
|
||||
|
@@ -1,6 +1,7 @@
|
||||
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 { z } from "zod";
|
||||
|
||||
export interface ServerConfig {
|
||||
@@ -79,6 +80,9 @@ export async function listPrimitives(client: Client) {
|
||||
}
|
||||
|
||||
/** Execute a request */
|
||||
export async function executeRequest(client: Client, request: any) {
|
||||
export async function executeRequest(
|
||||
client: Client,
|
||||
request: McpRequestMessage,
|
||||
) {
|
||||
return client.request(request, z.any());
|
||||
}
|
||||
|
61
app/mcp/types.ts
Normal file
61
app/mcp/types.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
// ref: https://spec.modelcontextprotocol.io/specification/basic/messages/
|
||||
|
||||
import { z } from "zod";
|
||||
|
||||
export interface McpRequestMessage {
|
||||
jsonrpc?: "2.0";
|
||||
id?: string | number;
|
||||
method: "tools/call" | string;
|
||||
params?: {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
export const McpRequestMessageSchema: z.ZodType<McpRequestMessage> = z.object({
|
||||
jsonrpc: z.literal("2.0").optional(),
|
||||
id: z.union([z.string(), z.number()]).optional(),
|
||||
method: z.string(),
|
||||
params: z.record(z.unknown()).optional(),
|
||||
});
|
||||
|
||||
export interface McpResponseMessage {
|
||||
jsonrpc?: "2.0";
|
||||
id?: string | number;
|
||||
result?: {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
error?: {
|
||||
code: number;
|
||||
message: string;
|
||||
data?: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
export const McpResponseMessageSchema: z.ZodType<McpResponseMessage> = z.object(
|
||||
{
|
||||
jsonrpc: z.literal("2.0").optional(),
|
||||
id: z.union([z.string(), z.number()]).optional(),
|
||||
result: z.record(z.unknown()).optional(),
|
||||
error: z
|
||||
.object({
|
||||
code: z.number(),
|
||||
message: z.string(),
|
||||
data: z.unknown().optional(),
|
||||
})
|
||||
.optional(),
|
||||
},
|
||||
);
|
||||
|
||||
export interface McpNotifications {
|
||||
jsonrpc?: "2.0";
|
||||
method: string;
|
||||
params?: {
|
||||
[key: string]: unknown;
|
||||
};
|
||||
}
|
||||
|
||||
export const McpNotificationsSchema: z.ZodType<McpNotifications> = z.object({
|
||||
jsonrpc: z.literal("2.0").optional(),
|
||||
method: z.string(),
|
||||
params: z.record(z.unknown()).optional(),
|
||||
});
|
Reference in New Issue
Block a user