types.ts
3.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
// ref: https://spec.modelcontextprotocol.io/specification/basic/messages/
import { z } from "zod";
import { Client } from "@modelcontextprotocol/sdk/client/index.js";
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(),
});
////////////
// Next Chat
////////////
export interface ListToolsResponse {
tools: {
name?: string;
description?: string;
inputSchema?: object;
[key: string]: any;
};
}
export type McpClientData =
| McpActiveClient
| McpErrorClient
| McpInitializingClient;
interface McpInitializingClient {
client: null;
tools: null;
errorMsg: null;
}
interface McpActiveClient {
client: Client;
tools: ListToolsResponse;
errorMsg: null;
}
interface McpErrorClient {
client: null;
tools: null;
errorMsg: string;
}
// 服务器状态类型
export type ServerStatus =
| "undefined"
| "active"
| "paused"
| "error"
| "initializing";
export interface ServerStatusResponse {
status: ServerStatus;
errorMsg: string | null;
}
// MCP 服务器配置相关类型
export interface ServerConfig {
command: string;
args: string[];
env?: Record<string, string>;
status?: "active" | "paused" | "error";
}
export interface McpConfigData {
// MCP Server 的配置
mcpServers: Record<string, ServerConfig>;
}
export const DEFAULT_MCP_CONFIG: McpConfigData = {
mcpServers: {},
};
export interface ArgsMapping {
// 参数映射的类型
type: "spread" | "single" | "env";
// 参数映射的位置
position?: number;
// 参数映射的 key
key?: string;
}
export interface PresetServer {
// MCP Server 的唯一标识,作为最终配置文件 Json 的 key
id: string;
// MCP Server 的显示名称
name: string;
// MCP Server 的描述
description: string;
// MCP Server 的仓库地址
repo: string;
// MCP Server 的标签
tags: string[];
// MCP Server 的命令
command: string;
// MCP Server 的参数
baseArgs: string[];
// MCP Server 是否需要配置
configurable: boolean;
// MCP Server 的配置 schema
configSchema?: {
properties: Record<
string,
{
type: string;
description?: string;
required?: boolean;
minItems?: number;
}
>;
};
// MCP Server 的参数映射
argsMapping?: Record<string, ArgsMapping>;
}