|
| 1 | +import qs from 'qs'; |
1 | 2 | import yargs from 'yargs';
|
2 | 3 | import { hideBin } from 'yargs/helpers';
|
| 4 | +import z from 'zod'; |
3 | 5 | import { endpoints, Filter } from './tools';
|
4 | 6 | import { ClientCapabilities, knownClients, ClientType } from './compat';
|
5 | 7 |
|
@@ -47,7 +49,7 @@ function parseCapabilityValue(cap: string): { name: Capability; value?: number }
|
47 | 49 | return { name: cap as Capability };
|
48 | 50 | }
|
49 | 51 |
|
50 |
| -export function parseOptions(): CLIOptions { |
| 52 | +export function parseCLIOptions(): CLIOptions { |
51 | 53 | const opts = yargs(hideBin(process.argv))
|
52 | 54 | .option('tools', {
|
53 | 55 | type: 'string',
|
@@ -271,6 +273,123 @@ export function parseOptions(): CLIOptions {
|
271 | 273 | };
|
272 | 274 | }
|
273 | 275 |
|
| 276 | +const coerceArray = <T extends z.ZodTypeAny>(zodType: T) => |
| 277 | + z.preprocess( |
| 278 | + (val) => |
| 279 | + Array.isArray(val) ? val |
| 280 | + : val ? [val] |
| 281 | + : val, |
| 282 | + z.array(zodType).optional(), |
| 283 | + ); |
| 284 | + |
| 285 | +const QueryOptions = z.object({ |
| 286 | + tools: coerceArray(z.enum(['dynamic', 'all'])).describe('Use dynamic tools or all tools'), |
| 287 | + no_tools: coerceArray(z.enum(['dynamic', 'all'])).describe('Do not use dynamic tools or all tools'), |
| 288 | + tool: coerceArray(z.string()).describe('Include tools matching the specified names'), |
| 289 | + resource: coerceArray(z.string()).describe('Include tools matching the specified resources'), |
| 290 | + operation: coerceArray(z.enum(['read', 'write'])).describe( |
| 291 | + 'Include tools matching the specified operations', |
| 292 | + ), |
| 293 | + tag: coerceArray(z.string()).describe('Include tools with the specified tags'), |
| 294 | + no_tool: coerceArray(z.string()).describe('Exclude tools matching the specified names'), |
| 295 | + no_resource: coerceArray(z.string()).describe('Exclude tools matching the specified resources'), |
| 296 | + no_operation: coerceArray(z.enum(['read', 'write'])).describe( |
| 297 | + 'Exclude tools matching the specified operations', |
| 298 | + ), |
| 299 | + no_tag: coerceArray(z.string()).describe('Exclude tools with the specified tags'), |
| 300 | + client: ClientType.optional().describe('Specify the MCP client being used'), |
| 301 | + capability: coerceArray(z.string()).describe('Specify client capabilities'), |
| 302 | + no_capability: coerceArray(z.enum(CAPABILITY_CHOICES)).describe('Unset client capabilities'), |
| 303 | +}); |
| 304 | + |
| 305 | +export function parseQueryOptions(defaultOptions: McpOptions, query: unknown): McpOptions { |
| 306 | + const queryObject = typeof query === 'string' ? qs.parse(query) : query; |
| 307 | + const queryOptions = QueryOptions.parse(queryObject); |
| 308 | + |
| 309 | + const filters: Filter[] = [...defaultOptions.filters]; |
| 310 | + |
| 311 | + for (const resource of queryOptions.resource || []) { |
| 312 | + filters.push({ type: 'resource', op: 'include', value: resource }); |
| 313 | + } |
| 314 | + for (const operation of queryOptions.operation || []) { |
| 315 | + filters.push({ type: 'operation', op: 'include', value: operation }); |
| 316 | + } |
| 317 | + for (const tag of queryOptions.tag || []) { |
| 318 | + filters.push({ type: 'tag', op: 'include', value: tag }); |
| 319 | + } |
| 320 | + for (const tool of queryOptions.tool || []) { |
| 321 | + filters.push({ type: 'tool', op: 'include', value: tool }); |
| 322 | + } |
| 323 | + for (const resource of queryOptions.no_resource || []) { |
| 324 | + filters.push({ type: 'resource', op: 'exclude', value: resource }); |
| 325 | + } |
| 326 | + for (const operation of queryOptions.no_operation || []) { |
| 327 | + filters.push({ type: 'operation', op: 'exclude', value: operation }); |
| 328 | + } |
| 329 | + for (const tag of queryOptions.no_tag || []) { |
| 330 | + filters.push({ type: 'tag', op: 'exclude', value: tag }); |
| 331 | + } |
| 332 | + for (const tool of queryOptions.no_tool || []) { |
| 333 | + filters.push({ type: 'tool', op: 'exclude', value: tool }); |
| 334 | + } |
| 335 | + |
| 336 | + // Parse client capabilities |
| 337 | + const clientCapabilities: ClientCapabilities = { |
| 338 | + topLevelUnions: true, |
| 339 | + validJson: true, |
| 340 | + refs: true, |
| 341 | + unions: true, |
| 342 | + formats: true, |
| 343 | + toolNameLength: undefined, |
| 344 | + ...defaultOptions.capabilities, |
| 345 | + }; |
| 346 | + |
| 347 | + for (const cap of queryOptions.capability || []) { |
| 348 | + const parsed = parseCapabilityValue(cap); |
| 349 | + if (parsed.name === 'top-level-unions') { |
| 350 | + clientCapabilities.topLevelUnions = true; |
| 351 | + } else if (parsed.name === 'valid-json') { |
| 352 | + clientCapabilities.validJson = true; |
| 353 | + } else if (parsed.name === 'refs') { |
| 354 | + clientCapabilities.refs = true; |
| 355 | + } else if (parsed.name === 'unions') { |
| 356 | + clientCapabilities.unions = true; |
| 357 | + } else if (parsed.name === 'formats') { |
| 358 | + clientCapabilities.formats = true; |
| 359 | + } else if (parsed.name === 'tool-name-length') { |
| 360 | + clientCapabilities.toolNameLength = parsed.value; |
| 361 | + } |
| 362 | + } |
| 363 | + |
| 364 | + for (const cap of queryOptions.no_capability || []) { |
| 365 | + if (cap === 'top-level-unions') { |
| 366 | + clientCapabilities.topLevelUnions = false; |
| 367 | + } else if (cap === 'valid-json') { |
| 368 | + clientCapabilities.validJson = false; |
| 369 | + } else if (cap === 'refs') { |
| 370 | + clientCapabilities.refs = false; |
| 371 | + } else if (cap === 'unions') { |
| 372 | + clientCapabilities.unions = false; |
| 373 | + } else if (cap === 'formats') { |
| 374 | + clientCapabilities.formats = false; |
| 375 | + } else if (cap === 'tool-name-length') { |
| 376 | + clientCapabilities.toolNameLength = undefined; |
| 377 | + } |
| 378 | + } |
| 379 | + |
| 380 | + return { |
| 381 | + client: queryOptions.client ?? defaultOptions.client, |
| 382 | + includeDynamicTools: |
| 383 | + defaultOptions.includeDynamicTools ?? |
| 384 | + (queryOptions.tools?.includes('dynamic') && !queryOptions.no_tools?.includes('dynamic')), |
| 385 | + includeAllTools: |
| 386 | + defaultOptions.includeAllTools ?? |
| 387 | + (queryOptions.tools?.includes('all') && !queryOptions.no_tools?.includes('all')), |
| 388 | + filters, |
| 389 | + capabilities: clientCapabilities, |
| 390 | + }; |
| 391 | +} |
| 392 | + |
274 | 393 | function getCapabilitiesExplanation(): string {
|
275 | 394 | return `
|
276 | 395 | Client Capabilities Explanation:
|
|
0 commit comments