@wincode/mcp 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +57 -2
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +34 -4
- package/src/client.ts +370 -0
- package/src/config/resolve.ts +722 -0
- package/src/config/schema.ts +96 -0
- package/src/config.ts +31 -0
- package/src/identifiers.ts +6 -0
- package/src/index.ts +10 -0
- package/src/management.ts +257 -0
- package/src/manifest.ts +102 -0
- package/src/oauth.ts +347 -0
- package/src/plugin/config.ts +40 -0
- package/src/plugin/index.ts +501 -0
- package/src/registry.ts +1455 -0
- package/src/result.ts +504 -0
- package/src/sanitize.ts +32 -0
- package/src/tool-identity.ts +24 -0
- package/src/utf8-byte-length.ts +4 -0
- package/test/client.test.ts +642 -0
- package/test/config-resolve.test.ts +376 -0
- package/test/management.test.ts +186 -0
- package/test/manifest.test.ts +17 -0
- package/test/oauth.test.ts +106 -0
- package/test/plugin-config.test.ts +117 -0
- package/test/result.test.ts +291 -0
- package/test/sanitize.test.ts +103 -0
- package/test/tool-identity.test.ts +27 -0
- package/tsconfig.json +15 -0
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import type { SetRequired } from "type-fest";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
|
|
4
|
+
export const mcpServerNameSchema = z.string().regex(/^[A-Za-z0-9_-]+$/u);
|
|
5
|
+
|
|
6
|
+
export const phaseTimeoutSchema = z.number().int().positive();
|
|
7
|
+
export const timeoutPatchSchema = z
|
|
8
|
+
.object({
|
|
9
|
+
startup: phaseTimeoutSchema.optional(),
|
|
10
|
+
catalog: phaseTimeoutSchema.optional(),
|
|
11
|
+
execution: phaseTimeoutSchema.optional(),
|
|
12
|
+
})
|
|
13
|
+
.strict();
|
|
14
|
+
|
|
15
|
+
export const mcpExposureSchema = z.enum(["direct", "hidden"]);
|
|
16
|
+
const toolExposureSchema = z.record(z.string(), mcpExposureSchema);
|
|
17
|
+
const oauthSchema = z.union([
|
|
18
|
+
z.literal(false),
|
|
19
|
+
z.record(z.string(), z.unknown()),
|
|
20
|
+
]);
|
|
21
|
+
|
|
22
|
+
/** Pi-shaped entry accepted from a top-level `mcpServers` document. */
|
|
23
|
+
export const rawServerSchema = z
|
|
24
|
+
.object({
|
|
25
|
+
type: z.enum(["stdio", "http", "streamable-http"]).optional(),
|
|
26
|
+
command: z.string().trim().min(1).optional(),
|
|
27
|
+
args: z.array(z.string()).optional(),
|
|
28
|
+
cwd: z.string().optional(),
|
|
29
|
+
env: z.record(z.string(), z.string()).optional(),
|
|
30
|
+
url: z.string().optional(),
|
|
31
|
+
headers: z.record(z.string(), z.string()).optional(),
|
|
32
|
+
auth: z.enum(["oauth"]).optional(),
|
|
33
|
+
oauth: oauthSchema.optional(),
|
|
34
|
+
enabled: z.boolean().optional(),
|
|
35
|
+
timeout: z.number().positive().optional(),
|
|
36
|
+
description: z.string().optional(),
|
|
37
|
+
exposure: mcpExposureSchema.optional(),
|
|
38
|
+
toolExposure: toolExposureSchema.optional(),
|
|
39
|
+
})
|
|
40
|
+
.strict();
|
|
41
|
+
|
|
42
|
+
export const localServerSchema = z.object({
|
|
43
|
+
name: z.string(),
|
|
44
|
+
type: z.literal("local"),
|
|
45
|
+
command: z.array(z.string()).min(1),
|
|
46
|
+
cwd: z.string().optional(),
|
|
47
|
+
environment: z.record(z.string(), z.string()).optional(),
|
|
48
|
+
description: z.string().optional(),
|
|
49
|
+
disabled: z.boolean(),
|
|
50
|
+
exposure: mcpExposureSchema.optional(),
|
|
51
|
+
toolExposure: toolExposureSchema.optional(),
|
|
52
|
+
timeout: z.object({
|
|
53
|
+
startup: phaseTimeoutSchema,
|
|
54
|
+
catalog: phaseTimeoutSchema,
|
|
55
|
+
execution: phaseTimeoutSchema,
|
|
56
|
+
}),
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
export const remoteServerSchema = z.object({
|
|
60
|
+
name: z.string(),
|
|
61
|
+
type: z.literal("remote"),
|
|
62
|
+
url: z.string(),
|
|
63
|
+
headers: z.record(z.string(), z.string()).optional(),
|
|
64
|
+
auth: z.enum(["oauth"]).optional(),
|
|
65
|
+
oauth: oauthSchema.optional(),
|
|
66
|
+
description: z.string().optional(),
|
|
67
|
+
disabled: z.boolean(),
|
|
68
|
+
exposure: mcpExposureSchema.optional(),
|
|
69
|
+
toolExposure: toolExposureSchema.optional(),
|
|
70
|
+
timeout: z.object({
|
|
71
|
+
startup: phaseTimeoutSchema,
|
|
72
|
+
catalog: phaseTimeoutSchema,
|
|
73
|
+
execution: phaseTimeoutSchema,
|
|
74
|
+
}),
|
|
75
|
+
});
|
|
76
|
+
|
|
77
|
+
export const resolvedServerSchema = z.discriminatedUnion("type", [
|
|
78
|
+
localServerSchema,
|
|
79
|
+
remoteServerSchema,
|
|
80
|
+
]);
|
|
81
|
+
|
|
82
|
+
export type McpExposure = z.infer<typeof mcpExposureSchema>;
|
|
83
|
+
export type McpTimeouts = SetRequired<
|
|
84
|
+
z.infer<typeof timeoutPatchSchema>,
|
|
85
|
+
"startup" | "catalog" | "execution"
|
|
86
|
+
>;
|
|
87
|
+
export type LocalMcpServerConfig = z.infer<typeof localServerSchema>;
|
|
88
|
+
export type RemoteMcpServerConfig = z.infer<typeof remoteServerSchema>;
|
|
89
|
+
export type ResolvedMcpServerConfig = z.infer<typeof resolvedServerSchema>;
|
|
90
|
+
export type RawMcpServerConfig = z.infer<typeof rawServerSchema>;
|
|
91
|
+
|
|
92
|
+
export const DEFAULT_MCP_TIMEOUTS = {
|
|
93
|
+
startup: 30_000,
|
|
94
|
+
catalog: 30_000,
|
|
95
|
+
execution: 60_000,
|
|
96
|
+
} as const;
|
package/src/config.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { McpConfigResult } from "./config/resolve";
|
|
2
|
+
|
|
3
|
+
export {
|
|
4
|
+
type InvalidMcpServerConfig,
|
|
5
|
+
type McpConfigDiagnostic,
|
|
6
|
+
type McpConfigDiagnosticCode,
|
|
7
|
+
type McpConfigOrigin,
|
|
8
|
+
type McpConfigResult,
|
|
9
|
+
type McpConfigSnapshot,
|
|
10
|
+
type McpConfigSource,
|
|
11
|
+
resolveServers as resolveMcpConfig,
|
|
12
|
+
} from "./config/resolve";
|
|
13
|
+
export {
|
|
14
|
+
DEFAULT_MCP_TIMEOUTS,
|
|
15
|
+
type LocalMcpServerConfig,
|
|
16
|
+
type McpTimeouts,
|
|
17
|
+
type RemoteMcpServerConfig,
|
|
18
|
+
type ResolvedMcpServerConfig,
|
|
19
|
+
} from "./config/schema";
|
|
20
|
+
|
|
21
|
+
/** Inputs the application supplies when the MCP runtime loads its config. */
|
|
22
|
+
export type McpConfigLoadRequest = Readonly<{
|
|
23
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
24
|
+
refresh: boolean;
|
|
25
|
+
workspace: string;
|
|
26
|
+
}>;
|
|
27
|
+
|
|
28
|
+
/** Application-owned source for Wincode configuration snapshots. */
|
|
29
|
+
export type McpConfigLoader = (
|
|
30
|
+
request: McpConfigLoadRequest
|
|
31
|
+
) => Promise<McpConfigResult>;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from "./client";
|
|
2
|
+
export * from "./config";
|
|
3
|
+
export * from "./identifiers";
|
|
4
|
+
export * from "./management";
|
|
5
|
+
export * from "./manifest";
|
|
6
|
+
export * from "./oauth";
|
|
7
|
+
export * from "./registry";
|
|
8
|
+
export * from "./result";
|
|
9
|
+
export * from "./sanitize";
|
|
10
|
+
export * from "./tool-identity";
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type McpConfigResult,
|
|
3
|
+
type McpConfigSnapshot,
|
|
4
|
+
resolveMcpConfig,
|
|
5
|
+
} from "./config";
|
|
6
|
+
import {
|
|
7
|
+
mcpServerNameSchema,
|
|
8
|
+
type RawMcpServerConfig,
|
|
9
|
+
rawServerSchema,
|
|
10
|
+
} from "./config/schema";
|
|
11
|
+
import {
|
|
12
|
+
loginMcpServer,
|
|
13
|
+
logoutMcpServer,
|
|
14
|
+
type McpOAuthLoginInput,
|
|
15
|
+
type McpSecretStore,
|
|
16
|
+
} from "./oauth";
|
|
17
|
+
|
|
18
|
+
export type McpManagementScope = "global" | "project";
|
|
19
|
+
|
|
20
|
+
export type McpManagementConfigAccess = Readonly<{
|
|
21
|
+
getSnapshot: () => Promise<McpConfigSnapshot>;
|
|
22
|
+
setValue: (
|
|
23
|
+
scope: McpManagementScope,
|
|
24
|
+
path: readonly string[],
|
|
25
|
+
value: unknown
|
|
26
|
+
) => Promise<void>;
|
|
27
|
+
}>;
|
|
28
|
+
|
|
29
|
+
export type McpManagementAddInput = Readonly<{
|
|
30
|
+
args?: readonly string[];
|
|
31
|
+
auth?: "oauth";
|
|
32
|
+
command?: string;
|
|
33
|
+
cwd?: string;
|
|
34
|
+
env?: Readonly<Record<string, string>>;
|
|
35
|
+
headers?: Readonly<Record<string, string>>;
|
|
36
|
+
name: string;
|
|
37
|
+
scope: McpManagementScope;
|
|
38
|
+
url?: string;
|
|
39
|
+
}>;
|
|
40
|
+
|
|
41
|
+
export type McpManagementLoginInput = Readonly<{
|
|
42
|
+
name: string;
|
|
43
|
+
openAuthorizationUrl: McpOAuthLoginInput["openAuthorizationUrl"];
|
|
44
|
+
readRedirectUrl?: McpOAuthLoginInput["readRedirectUrl"];
|
|
45
|
+
secrets: McpSecretStore;
|
|
46
|
+
}>;
|
|
47
|
+
|
|
48
|
+
export type McpManagementLogoutInput = Readonly<{
|
|
49
|
+
name: string;
|
|
50
|
+
secrets: McpSecretStore;
|
|
51
|
+
}>;
|
|
52
|
+
|
|
53
|
+
export type McpManagementOAuthOperations = Readonly<{
|
|
54
|
+
login: (input: McpOAuthLoginInput) => Promise<void>;
|
|
55
|
+
logout: (secrets: McpSecretStore, serverName: string) => Promise<void>;
|
|
56
|
+
}>;
|
|
57
|
+
|
|
58
|
+
export type McpManagementApi = Readonly<{
|
|
59
|
+
add: (input: McpManagementAddInput) => Promise<void>;
|
|
60
|
+
list: () => Promise<McpConfigResult>;
|
|
61
|
+
login: (input: McpManagementLoginInput) => Promise<void>;
|
|
62
|
+
logout: (input: McpManagementLogoutInput) => Promise<void>;
|
|
63
|
+
remove: (
|
|
64
|
+
input: Readonly<{ name: string; scope?: McpManagementScope }>
|
|
65
|
+
) => Promise<McpManagementScope>;
|
|
66
|
+
}>;
|
|
67
|
+
|
|
68
|
+
export type McpManagementDependencies = Readonly<{
|
|
69
|
+
authorizeWrite: (scope: McpManagementScope) => void;
|
|
70
|
+
config: McpManagementConfigAccess;
|
|
71
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
72
|
+
oauth?: McpManagementOAuthOperations;
|
|
73
|
+
workspace: string;
|
|
74
|
+
}>;
|
|
75
|
+
|
|
76
|
+
const validateScope = (scope: string): void => {
|
|
77
|
+
if (scope !== "global" && scope !== "project") {
|
|
78
|
+
throw new Error("MCP config scope must be user or project.");
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
const validateName = (name: string): void => {
|
|
83
|
+
if (!mcpServerNameSchema.safeParse(name).success) {
|
|
84
|
+
throw new Error(
|
|
85
|
+
"Server names may contain only letters, digits, underscores, and hyphens."
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
};
|
|
89
|
+
|
|
90
|
+
const hasServerInScope = (
|
|
91
|
+
snapshot: McpConfigSnapshot,
|
|
92
|
+
scope: McpManagementScope,
|
|
93
|
+
name: string
|
|
94
|
+
): boolean =>
|
|
95
|
+
snapshot.sources.some((source) => {
|
|
96
|
+
if (source.scope !== scope) {
|
|
97
|
+
return false;
|
|
98
|
+
}
|
|
99
|
+
const servers = source.document.mcpServers;
|
|
100
|
+
return (
|
|
101
|
+
typeof servers === "object" &&
|
|
102
|
+
servers !== null &&
|
|
103
|
+
!Array.isArray(servers) &&
|
|
104
|
+
Object.hasOwn(servers, name)
|
|
105
|
+
);
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
const assertValidAddOptions = (input: McpManagementAddInput): void => {
|
|
109
|
+
const hasCommand = input.command !== undefined;
|
|
110
|
+
const hasUrl = input.url !== undefined;
|
|
111
|
+
if (hasCommand === hasUrl) {
|
|
112
|
+
throw new Error("Provide exactly one of --command or --url.");
|
|
113
|
+
}
|
|
114
|
+
if (hasCommand && Object.keys(input.headers ?? {}).length > 0) {
|
|
115
|
+
throw new Error("--header can be used only with --url.");
|
|
116
|
+
}
|
|
117
|
+
if (
|
|
118
|
+
hasUrl &&
|
|
119
|
+
((input.args?.length ?? 0) > 0 ||
|
|
120
|
+
input.cwd !== undefined ||
|
|
121
|
+
Object.keys(input.env ?? {}).length > 0)
|
|
122
|
+
) {
|
|
123
|
+
throw new Error("--arg, --cwd, and --env can be used only with --command.");
|
|
124
|
+
}
|
|
125
|
+
if (hasCommand && input.auth !== undefined) {
|
|
126
|
+
throw new Error("OAuth authentication can be used only with --url.");
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const rawServerConfigFor = (
|
|
131
|
+
input: McpManagementAddInput
|
|
132
|
+
): RawMcpServerConfig =>
|
|
133
|
+
input.command === undefined
|
|
134
|
+
? {
|
|
135
|
+
url: input.url,
|
|
136
|
+
...(input.headers === undefined ||
|
|
137
|
+
Object.keys(input.headers).length === 0
|
|
138
|
+
? {}
|
|
139
|
+
: { headers: { ...input.headers } }),
|
|
140
|
+
...(input.auth === undefined ? {} : { auth: input.auth }),
|
|
141
|
+
}
|
|
142
|
+
: {
|
|
143
|
+
command: input.command,
|
|
144
|
+
...(input.args === undefined || input.args.length === 0
|
|
145
|
+
? {}
|
|
146
|
+
: { args: [...input.args] }),
|
|
147
|
+
...(input.cwd === undefined ? {} : { cwd: input.cwd }),
|
|
148
|
+
...(input.env === undefined || Object.keys(input.env).length === 0
|
|
149
|
+
? {}
|
|
150
|
+
: { env: { ...input.env } }),
|
|
151
|
+
};
|
|
152
|
+
|
|
153
|
+
const rawServerFor = (input: McpManagementAddInput): RawMcpServerConfig => {
|
|
154
|
+
assertValidAddOptions(input);
|
|
155
|
+
const parsed = rawServerSchema.safeParse(rawServerConfigFor(input));
|
|
156
|
+
if (!parsed.success) {
|
|
157
|
+
throw new Error(
|
|
158
|
+
`Invalid MCP server configuration: ${parsed.error.issues
|
|
159
|
+
.map(({ message }) => message)
|
|
160
|
+
.join("; ")}`
|
|
161
|
+
);
|
|
162
|
+
}
|
|
163
|
+
return parsed.data;
|
|
164
|
+
};
|
|
165
|
+
|
|
166
|
+
export const createMcpManagement = ({
|
|
167
|
+
authorizeWrite,
|
|
168
|
+
config,
|
|
169
|
+
env,
|
|
170
|
+
oauth = { login: loginMcpServer, logout: logoutMcpServer },
|
|
171
|
+
workspace,
|
|
172
|
+
}: McpManagementDependencies): McpManagementApi => {
|
|
173
|
+
const resolveConfig = async (): Promise<{
|
|
174
|
+
result: McpConfigResult;
|
|
175
|
+
snapshot: McpConfigSnapshot;
|
|
176
|
+
}> => {
|
|
177
|
+
const snapshot = await config.getSnapshot();
|
|
178
|
+
return {
|
|
179
|
+
result: await resolveMcpConfig({ env, snapshot, workspace }),
|
|
180
|
+
snapshot,
|
|
181
|
+
};
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const oauthServer = async (name: string) => {
|
|
185
|
+
validateName(name);
|
|
186
|
+
const { result } = await resolveConfig();
|
|
187
|
+
const server = result.servers[name];
|
|
188
|
+
if (
|
|
189
|
+
server === undefined ||
|
|
190
|
+
server.type !== "remote" ||
|
|
191
|
+
server.auth !== "oauth"
|
|
192
|
+
) {
|
|
193
|
+
throw new Error(`MCP Server '${name}' is not a configured OAuth server.`);
|
|
194
|
+
}
|
|
195
|
+
return server;
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
add: async (input) => {
|
|
200
|
+
validateScope(input.scope);
|
|
201
|
+
validateName(input.name);
|
|
202
|
+
const rawServer = rawServerFor(input);
|
|
203
|
+
authorizeWrite(input.scope);
|
|
204
|
+
const snapshot = await config.getSnapshot();
|
|
205
|
+
if (hasServerInScope(snapshot, input.scope, input.name)) {
|
|
206
|
+
const label = input.scope === "global" ? "user" : "project";
|
|
207
|
+
throw new Error(
|
|
208
|
+
`MCP Server '${input.name}' already exists in ${label} config.`
|
|
209
|
+
);
|
|
210
|
+
}
|
|
211
|
+
await config.setValue(input.scope, ["mcpServers", input.name], rawServer);
|
|
212
|
+
},
|
|
213
|
+
list: async () => (await resolveConfig()).result,
|
|
214
|
+
login: async (input) => {
|
|
215
|
+
const server = await oauthServer(input.name);
|
|
216
|
+
await oauth.login({
|
|
217
|
+
openAuthorizationUrl: input.openAuthorizationUrl,
|
|
218
|
+
oauth: server.oauth,
|
|
219
|
+
readRedirectUrl: input.readRedirectUrl,
|
|
220
|
+
secrets: input.secrets,
|
|
221
|
+
serverName: input.name,
|
|
222
|
+
serverUrl: server.url,
|
|
223
|
+
});
|
|
224
|
+
},
|
|
225
|
+
logout: async (input) => {
|
|
226
|
+
await oauthServer(input.name);
|
|
227
|
+
await oauth.logout(input.secrets, input.name);
|
|
228
|
+
},
|
|
229
|
+
remove: async ({ name, scope: requestedScope }) => {
|
|
230
|
+
validateName(name);
|
|
231
|
+
if (requestedScope !== undefined) {
|
|
232
|
+
validateScope(requestedScope);
|
|
233
|
+
authorizeWrite(requestedScope);
|
|
234
|
+
}
|
|
235
|
+
const { result, snapshot } = await resolveConfig();
|
|
236
|
+
if (result.servers[name] === undefined) {
|
|
237
|
+
throw new Error(`MCP Server '${name}' does not exist.`);
|
|
238
|
+
}
|
|
239
|
+
const scope =
|
|
240
|
+
requestedScope ??
|
|
241
|
+
(result.serverSources?.[name]?.scope === "project"
|
|
242
|
+
? "project"
|
|
243
|
+
: "global");
|
|
244
|
+
if (!hasServerInScope(snapshot, scope, name)) {
|
|
245
|
+
const label = scope === "global" ? "user" : "project";
|
|
246
|
+
throw new Error(
|
|
247
|
+
`MCP Server '${name}' does not exist in ${label} config.`
|
|
248
|
+
);
|
|
249
|
+
}
|
|
250
|
+
if (requestedScope === undefined) {
|
|
251
|
+
authorizeWrite(scope);
|
|
252
|
+
}
|
|
253
|
+
await config.setValue(scope, ["mcpServers", name], undefined);
|
|
254
|
+
return scope;
|
|
255
|
+
},
|
|
256
|
+
};
|
|
257
|
+
};
|
package/src/manifest.ts
ADDED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import {
|
|
2
|
+
isObjectLike,
|
|
3
|
+
isJsonObject as isRuntimeJsonObject,
|
|
4
|
+
isJsonValue as isRuntimeJsonValue,
|
|
5
|
+
isString,
|
|
6
|
+
} from "@wincode/utils";
|
|
7
|
+
import type { JsonObject, JsonValue, UnknownRecord } from "type-fest";
|
|
8
|
+
import { z } from "zod";
|
|
9
|
+
import { utf8ByteLength } from "./utf8-byte-length";
|
|
10
|
+
|
|
11
|
+
export const MAX_MCP_TOOL_COUNT = 128;
|
|
12
|
+
export const MAX_MCP_TOOL_NAME_LENGTH = 64;
|
|
13
|
+
export const MAX_MCP_TOOL_DESCRIPTION_BYTES = 8 * 1024;
|
|
14
|
+
export const MAX_MCP_TOOL_SCHEMA_BYTES = 64 * 1024;
|
|
15
|
+
export const MAX_MCP_MANIFEST_BYTES = 256 * 1024;
|
|
16
|
+
export const MAX_MCP_RESULT_BYTES = 48 * 1024;
|
|
17
|
+
export const MCP_TOOL_NAME_REGEX = /^[A-Za-z0-9_-]+$/u;
|
|
18
|
+
|
|
19
|
+
const MAX_JSON_NESTING_DEPTH = 64;
|
|
20
|
+
|
|
21
|
+
export const isJsonValue = (value: unknown): value is JsonValue =>
|
|
22
|
+
isRuntimeJsonValue(value, { maxDepth: MAX_JSON_NESTING_DEPTH });
|
|
23
|
+
|
|
24
|
+
export const isJsonObject = (value: unknown): value is JsonObject =>
|
|
25
|
+
isRuntimeJsonObject(value, { maxDepth: MAX_JSON_NESTING_DEPTH });
|
|
26
|
+
|
|
27
|
+
export type McpToolManifestEntry = {
|
|
28
|
+
name: string;
|
|
29
|
+
description: string;
|
|
30
|
+
inputSchema: JsonObject;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export const mcpToolManifestEntrySchema: z.ZodType<McpToolManifestEntry> =
|
|
34
|
+
z.custom<McpToolManifestEntry>(
|
|
35
|
+
(value) => {
|
|
36
|
+
if (!isObjectLike(value)) {
|
|
37
|
+
return false;
|
|
38
|
+
}
|
|
39
|
+
const entry = value as UnknownRecord;
|
|
40
|
+
if (
|
|
41
|
+
Object.keys(entry).some(
|
|
42
|
+
(key) => !["name", "description", "inputSchema"].includes(key)
|
|
43
|
+
)
|
|
44
|
+
) {
|
|
45
|
+
return false;
|
|
46
|
+
}
|
|
47
|
+
if (
|
|
48
|
+
!isString(entry.name) ||
|
|
49
|
+
entry.name.length < 1 ||
|
|
50
|
+
entry.name.length > MAX_MCP_TOOL_NAME_LENGTH ||
|
|
51
|
+
!entry.name.startsWith("mcp_") ||
|
|
52
|
+
!MCP_TOOL_NAME_REGEX.test(entry.name)
|
|
53
|
+
) {
|
|
54
|
+
return false;
|
|
55
|
+
}
|
|
56
|
+
if (!isString(entry.description)) {
|
|
57
|
+
return false;
|
|
58
|
+
}
|
|
59
|
+
if (utf8ByteLength(entry.description) > MAX_MCP_TOOL_DESCRIPTION_BYTES) {
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
if (!isJsonObject(entry.inputSchema)) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
try {
|
|
66
|
+
if (
|
|
67
|
+
utf8ByteLength(JSON.stringify(entry.inputSchema)) >
|
|
68
|
+
MAX_MCP_TOOL_SCHEMA_BYTES
|
|
69
|
+
) {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
return false;
|
|
74
|
+
}
|
|
75
|
+
return true;
|
|
76
|
+
},
|
|
77
|
+
{ message: "invalid MCP tool manifest entry" }
|
|
78
|
+
);
|
|
79
|
+
|
|
80
|
+
export const mcpToolManifestSchema = z
|
|
81
|
+
.array(mcpToolManifestEntrySchema)
|
|
82
|
+
.max(MAX_MCP_TOOL_COUNT)
|
|
83
|
+
.superRefine((tools, context) => {
|
|
84
|
+
const names = new Set<string>();
|
|
85
|
+
for (const tool of tools) {
|
|
86
|
+
if (names.has(tool.name)) {
|
|
87
|
+
context.addIssue({
|
|
88
|
+
code: "custom",
|
|
89
|
+
message: `duplicate tool name: ${tool.name}`,
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
names.add(tool.name);
|
|
93
|
+
}
|
|
94
|
+
if (utf8ByteLength(JSON.stringify(tools)) > MAX_MCP_MANIFEST_BYTES) {
|
|
95
|
+
context.addIssue({
|
|
96
|
+
code: "custom",
|
|
97
|
+
message: "manifest exceeds byte limit",
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
export type McpToolManifest = z.infer<typeof mcpToolManifestSchema>;
|