From badfd82202b575f3984337b886b8e8d4ead557d3 Mon Sep 17 00:00:00 2001 From: Pavel Feldman Date: Wed, 13 Aug 2025 18:23:25 -0700 Subject: [PATCH] chore: move tool schema to mcp as it is used by all servers (#887) --- src/browserServerBackend.ts | 2 +- src/loopTools/DEPS.list | 1 - src/loopTools/main.ts | 2 +- src/loopTools/tool.ts | 2 +- src/mcp/tool.ts | 42 +++++++++++++++++++++++++++++++++++++ src/tools/tool.ts | 26 +---------------------- src/utils/fileUtils.ts | 6 ------ 7 files changed, 46 insertions(+), 35 deletions(-) create mode 100644 src/mcp/tool.ts diff --git a/src/browserServerBackend.ts b/src/browserServerBackend.ts index 25f68ef..e71a15d 100644 --- a/src/browserServerBackend.ts +++ b/src/browserServerBackend.ts @@ -22,7 +22,7 @@ import { Response } from './response.js'; import { SessionLog } from './sessionLog.js'; import { filteredTools } from './tools.js'; import { packageJSON } from './utils/package.js'; -import { toMcpTool } from './tools/tool.js'; +import { toMcpTool } from './mcp/tool.js'; import type { Tool } from './tools/tool.js'; import type { BrowserContextFactory } from './browserContextFactory.js'; diff --git a/src/loopTools/DEPS.list b/src/loopTools/DEPS.list index 9fe8b14..5cf594d 100644 --- a/src/loopTools/DEPS.list +++ b/src/loopTools/DEPS.list @@ -2,5 +2,4 @@ ../ ../loop/ ../mcp/ -../tools/ ../utils/ diff --git a/src/loopTools/main.ts b/src/loopTools/main.ts index eb8b1c5..a8ea803 100644 --- a/src/loopTools/main.ts +++ b/src/loopTools/main.ts @@ -22,7 +22,7 @@ import { packageJSON } from '../utils/package.js'; import { Context } from './context.js'; import { perform } from './perform.js'; import { snapshot } from './snapshot.js'; -import { toMcpTool } from '../tools/tool.js'; +import { toMcpTool } from '../mcp/tool.js'; import type { FullConfig } from '../config.js'; import type { ServerBackend } from '../mcp/server.js'; diff --git a/src/loopTools/tool.ts b/src/loopTools/tool.ts index 000f1cf..8ea56aa 100644 --- a/src/loopTools/tool.ts +++ b/src/loopTools/tool.ts @@ -17,7 +17,7 @@ import type { z } from 'zod'; import type * as mcpServer from '../mcp/server.js'; import type { Context } from './context.js'; -import type { ToolSchema } from '../tools/tool.js'; +import type { ToolSchema } from '../mcp/tool.js'; export type Tool = { diff --git a/src/mcp/tool.ts b/src/mcp/tool.ts new file mode 100644 index 0000000..aff266e --- /dev/null +++ b/src/mcp/tool.ts @@ -0,0 +1,42 @@ +/** + * Copyright (c) Microsoft Corporation. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +import { zodToJsonSchema } from 'zod-to-json-schema'; + +import type { z } from 'zod'; +import type * as mcpServer from './server.js'; + +export type ToolSchema = { + name: string; + title: string; + description: string; + inputSchema: Input; + type: 'readOnly' | 'destructive'; +}; + +export function toMcpTool(tool: ToolSchema): mcpServer.Tool { + return { + name: tool.name, + description: tool.description, + inputSchema: zodToJsonSchema(tool.inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'], + annotations: { + title: tool.title, + readOnlyHint: tool.type === 'readOnly', + destructiveHint: tool.type === 'destructive', + openWorldHint: true, + }, + }; +} diff --git a/src/tools/tool.ts b/src/tools/tool.ts index 68d727e..41995ec 100644 --- a/src/tools/tool.ts +++ b/src/tools/tool.ts @@ -14,15 +14,13 @@ * limitations under the License. */ -import { zodToJsonSchema } from 'zod-to-json-schema'; - import type { z } from 'zod'; import type { Context } from '../context.js'; import type * as playwright from 'playwright'; import type { ToolCapability } from '../../config.js'; import type { Tab } from '../tab.js'; import type { Response } from '../response.js'; -import type * as mcpServer from '../mcp/server.js'; +import type { ToolSchema } from '../mcp/tool.js'; export type FileUploadModalState = { type: 'fileChooser'; @@ -38,28 +36,6 @@ export type DialogModalState = { export type ModalState = FileUploadModalState | DialogModalState; -export type ToolSchema = { - name: string; - title: string; - description: string; - inputSchema: Input; - type: 'readOnly' | 'destructive'; -}; - -export function toMcpTool(tool: ToolSchema): mcpServer.Tool { - return { - name: tool.name, - description: tool.description, - inputSchema: zodToJsonSchema(tool.inputSchema, { strictUnions: true }) as mcpServer.Tool['inputSchema'], - annotations: { - title: tool.title, - readOnlyHint: tool.type === 'readOnly', - destructiveHint: tool.type === 'destructive', - openWorldHint: true, - }, - }; -} - export type Tool = { capability: ToolCapability; schema: ToolSchema; diff --git a/src/utils/fileUtils.ts b/src/utils/fileUtils.ts index 4ebf7e5..587db96 100644 --- a/src/utils/fileUtils.ts +++ b/src/utils/fileUtils.ts @@ -17,8 +17,6 @@ import os from 'node:os'; import path from 'node:path'; -import type { FullConfig } from '../config.js'; - export function cacheDir() { let cacheDirectory: string; if (process.platform === 'linux') @@ -32,10 +30,6 @@ export function cacheDir() { return path.join(cacheDirectory, 'ms-playwright'); } -export async function userDataDir(browserConfig: FullConfig['browser']) { - return path.join(cacheDir(), 'ms-playwright', `mcp-${browserConfig.launchOptions?.channel ?? browserConfig?.browserName}-profile`); -} - export function sanitizeForFilePath(s: string) { const sanitize = (s: string) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-'); const separator = s.lastIndexOf('.');