/
githubmirror
/
angular-cli
Обзор
Документация
Войти
/
githubmirror
/
angular-cli
Код
Запросы
0
Пакеты
0
Релизы
0
Аналитика
Безопасность
main
packages/angular/cli/src/commands/mcp/tools/tool-registry.ts
96 строк
3 KB
Charles Lyding
feat(@angular/cli): add `--root` command line option to `mcp` command
04 авг 2026, 19:37
04 авг 2026, 19:37
41555df
Код
Авторство
О чём код?
/** * @license * Copyright Google LLC All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.dev/license */ import type { McpServer, ServerContext, ToolAnnotations, ToolCallback, } from '@modelcontextprotocol/server'; import { type ZodRawShape, z } from 'zod'; import type { AngularWorkspace } from '../../../utilities/config'; import type { Devserver } from '../devserver'; import type { Host } from '../host'; export interface McpToolContext { server: McpServer; workspace?: AngularWorkspace; logger: { warn(text: string): void }; exampleDatabasePath?: string; devservers: Map<string, Devserver>; host: Host; roots?: string[]; } export type McpToolCallback<TInput extends ZodRawShape = ZodRawShape> = ( args: z.infer<z.ZodObject<TInput>>, ctx: ServerContext, ) => ReturnType<ToolCallback>; export type McpToolFactory<TInput extends ZodRawShape> = ( context: McpToolContext, ) => McpToolCallback<TInput> | Promise<McpToolCallback<TInput>>; export interface McpToolDeclaration<TInput extends ZodRawShape, TOutput extends ZodRawShape> { name: string; title?: string; description: string; annotations?: ToolAnnotations; inputSchema?: TInput; outputSchema?: TOutput; factory: McpToolFactory<TInput>; shouldRegister?: (context: McpToolContext) => boolean | Promise<boolean>; isReadOnly?: boolean; isLocalOnly?: boolean; } // eslint-disable-next-line @typescript-eslint/no-explicit-any export type AnyMcpToolDeclaration = McpToolDeclaration<any, any>; export function declareTool<TInput extends ZodRawShape, TOutput extends ZodRawShape>( declaration: McpToolDeclaration<TInput, TOutput>, ): McpToolDeclaration<TInput, TOutput> { return declaration; } export async function registerTools( server: McpServer, context: Omit<McpToolContext, 'server'>, declarations: AnyMcpToolDeclaration[], ): Promise<void> { for (const declaration of declarations) { const toolContext = { ...context, server }; if (declaration.shouldRegister && !(await declaration.shouldRegister(toolContext))) { continue; } const { name, factory, shouldRegister, isReadOnly, isLocalOnly, ...config } = declaration; const handler = await factory(toolContext); // Add declarative characteristics to annotations config.annotations ??= {}; if (isReadOnly !== undefined) { config.annotations.readOnlyHint = isReadOnly; } if (isLocalOnly !== undefined) { // openWorldHint: false means local only config.annotations.openWorldHint = !isLocalOnly; } server.registerTool( name, { ...config, inputSchema: config.inputSchema ? z.object(config.inputSchema) : undefined, outputSchema: config.outputSchema ? z.object(config.outputSchema) : undefined, }, handler, ); } }