chore: move tool schema to mcp as it is used by all servers (#887)

This commit is contained in:
Pavel Feldman
2025-08-13 18:23:25 -07:00
committed by GitHub
parent 12942b81d6
commit badfd82202
7 changed files with 46 additions and 35 deletions

View File

@@ -22,7 +22,7 @@ import { Response } from './response.js';
import { SessionLog } from './sessionLog.js'; import { SessionLog } from './sessionLog.js';
import { filteredTools } from './tools.js'; import { filteredTools } from './tools.js';
import { packageJSON } from './utils/package.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 { Tool } from './tools/tool.js';
import type { BrowserContextFactory } from './browserContextFactory.js'; import type { BrowserContextFactory } from './browserContextFactory.js';

View File

@@ -2,5 +2,4 @@
../ ../
../loop/ ../loop/
../mcp/ ../mcp/
../tools/
../utils/ ../utils/

View File

@@ -22,7 +22,7 @@ import { packageJSON } from '../utils/package.js';
import { Context } from './context.js'; import { Context } from './context.js';
import { perform } from './perform.js'; import { perform } from './perform.js';
import { snapshot } from './snapshot.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 { FullConfig } from '../config.js';
import type { ServerBackend } from '../mcp/server.js'; import type { ServerBackend } from '../mcp/server.js';

View File

@@ -17,7 +17,7 @@
import type { z } from 'zod'; import type { z } from 'zod';
import type * as mcpServer from '../mcp/server.js'; import type * as mcpServer from '../mcp/server.js';
import type { Context } from './context.js'; import type { Context } from './context.js';
import type { ToolSchema } from '../tools/tool.js'; import type { ToolSchema } from '../mcp/tool.js';
export type Tool<Input extends z.Schema = z.Schema> = { export type Tool<Input extends z.Schema = z.Schema> = {

42
src/mcp/tool.ts Normal file
View File

@@ -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<Input extends z.Schema> = {
name: string;
title: string;
description: string;
inputSchema: Input;
type: 'readOnly' | 'destructive';
};
export function toMcpTool(tool: ToolSchema<any>): 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,
},
};
}

View File

@@ -14,15 +14,13 @@
* limitations under the License. * limitations under the License.
*/ */
import { zodToJsonSchema } from 'zod-to-json-schema';
import type { z } from 'zod'; import type { z } from 'zod';
import type { Context } from '../context.js'; import type { Context } from '../context.js';
import type * as playwright from 'playwright'; import type * as playwright from 'playwright';
import type { ToolCapability } from '../../config.js'; import type { ToolCapability } from '../../config.js';
import type { Tab } from '../tab.js'; import type { Tab } from '../tab.js';
import type { Response } from '../response.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 = { export type FileUploadModalState = {
type: 'fileChooser'; type: 'fileChooser';
@@ -38,28 +36,6 @@ export type DialogModalState = {
export type ModalState = FileUploadModalState | DialogModalState; export type ModalState = FileUploadModalState | DialogModalState;
export type ToolSchema<Input extends z.Schema> = {
name: string;
title: string;
description: string;
inputSchema: Input;
type: 'readOnly' | 'destructive';
};
export function toMcpTool(tool: ToolSchema<any>): 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<Input extends z.Schema = z.Schema> = { export type Tool<Input extends z.Schema = z.Schema> = {
capability: ToolCapability; capability: ToolCapability;
schema: ToolSchema<Input>; schema: ToolSchema<Input>;

View File

@@ -17,8 +17,6 @@
import os from 'node:os'; import os from 'node:os';
import path from 'node:path'; import path from 'node:path';
import type { FullConfig } from '../config.js';
export function cacheDir() { export function cacheDir() {
let cacheDirectory: string; let cacheDirectory: string;
if (process.platform === 'linux') if (process.platform === 'linux')
@@ -32,10 +30,6 @@ export function cacheDir() {
return path.join(cacheDirectory, 'ms-playwright'); 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) { export function sanitizeForFilePath(s: string) {
const sanitize = (s: string) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-'); const sanitize = (s: string) => s.replace(/[\x00-\x2C\x2E-\x2F\x3A-\x40\x5B-\x60\x7B-\x7F]+/g, '-');
const separator = s.lastIndexOf('.'); const separator = s.lastIndexOf('.');