|
| 1 | +/** |
| 2 | + * Copyright (c) Microsoft Corporation. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +import type * as playwright from 'playwright'; |
| 18 | +import type { |
| 19 | + ImageContent, |
| 20 | + TextContent, |
| 21 | +} from '@modelcontextprotocol/sdk/types.js'; |
| 22 | +import type { Tab } from './tab.js'; |
| 23 | + |
| 24 | +/** |
| 25 | + * Tool capability types |
| 26 | + */ |
| 27 | +export type ToolCapability = |
| 28 | + | 'core' |
| 29 | + | 'tabs' |
| 30 | + | 'pdf' |
| 31 | + | 'history' |
| 32 | + | 'wait' |
| 33 | + | 'files' |
| 34 | + | 'install' |
| 35 | + | 'testing'; |
| 36 | + |
| 37 | +/** |
| 38 | + * Configuration for the MCP server |
| 39 | + */ |
| 40 | +export type FullConfig = { |
| 41 | + browser: { |
| 42 | + browserAgent?: string; |
| 43 | + browserName: 'chromium' | 'firefox' | 'webkit'; |
| 44 | + isolated?: boolean; |
| 45 | + userDataDir?: string; |
| 46 | + launchOptions?: playwright.LaunchOptions; |
| 47 | + contextOptions?: playwright.BrowserContextOptions; |
| 48 | + cdpEndpoint?: string; |
| 49 | + remoteEndpoint?: string; |
| 50 | + }; |
| 51 | + server?: { |
| 52 | + port?: number; |
| 53 | + host?: string; |
| 54 | + }; |
| 55 | + capabilities?: ToolCapability[]; |
| 56 | + vision?: boolean; |
| 57 | + saveTrace?: boolean; |
| 58 | + outputDir?: string; |
| 59 | + network?: { |
| 60 | + allowedOrigins?: string[]; |
| 61 | + blockedOrigins?: string[]; |
| 62 | + }; |
| 63 | + imageResponses?: 'allow' | 'omit' | 'auto'; |
| 64 | +}; |
| 65 | + |
| 66 | +/** |
| 67 | + * Modal state types for handling dialogs and file choosers |
| 68 | + */ |
| 69 | +export type FileUploadModalState = { |
| 70 | + type: 'fileChooser'; |
| 71 | + description: string; |
| 72 | + fileChooser: playwright.FileChooser; |
| 73 | +}; |
| 74 | + |
| 75 | +export type DialogModalState = { |
| 76 | + type: 'dialog'; |
| 77 | + description: string; |
| 78 | + dialog: playwright.Dialog; |
| 79 | +}; |
| 80 | + |
| 81 | +export type ModalState = FileUploadModalState | DialogModalState; |
| 82 | + |
| 83 | +/** |
| 84 | + * Tool action result type |
| 85 | + */ |
| 86 | +export type ToolActionResult = |
| 87 | + | { content?: (ImageContent | TextContent)[] } |
| 88 | + | undefined |
| 89 | + | void; |
| 90 | + |
| 91 | +/** |
| 92 | + * Tool result type |
| 93 | + */ |
| 94 | +export type ToolResult = { |
| 95 | + code: string[]; |
| 96 | + action?: () => Promise<ToolActionResult>; |
| 97 | + captureSnapshot: boolean; |
| 98 | + waitForNetwork: boolean; |
| 99 | + resultOverride?: ToolActionResult; |
| 100 | +}; |
| 101 | + |
| 102 | +/** |
| 103 | + * Tool schema type |
| 104 | + */ |
| 105 | +export type ToolSchema<Input = any> = { |
| 106 | + name: string; |
| 107 | + title: string; |
| 108 | + description: string; |
| 109 | + inputSchema: Input; |
| 110 | + type: 'readOnly' | 'destructive'; |
| 111 | +}; |
| 112 | + |
| 113 | +/** |
| 114 | + * Tool type definition |
| 115 | + */ |
| 116 | +export type Tool<Input = any> = { |
| 117 | + capability: ToolCapability; |
| 118 | + schema: ToolSchema<Input>; |
| 119 | + clearsModalState?: ModalState['type']; |
| 120 | + handle: (context: Context, params: any) => Promise<ToolResult>; |
| 121 | +}; |
| 122 | + |
| 123 | +/** |
| 124 | + * Context class type definition |
| 125 | + */ |
| 126 | +export type Context = { |
| 127 | + readonly tools: Tool[]; |
| 128 | + readonly config: FullConfig; |
| 129 | + clientVersion?: { name: string; version: string }; |
| 130 | + |
| 131 | + clientSupportsImages(): boolean; |
| 132 | + modalStates(): ModalState[]; |
| 133 | + setModalState(modalState: ModalState, inTab: Tab): void; |
| 134 | + clearModalState(modalState: ModalState): void; |
| 135 | + modalStatesMarkdown(): string[]; |
| 136 | + tabs(): Tab[]; |
| 137 | + currentTabOrDie(): Tab; |
| 138 | + newTab(): Promise<Tab>; |
| 139 | + selectTab(index: number): Promise<void>; |
| 140 | + ensureTab(): Promise<Tab>; |
| 141 | + listTabsMarkdown(): Promise<string>; |
| 142 | + closeTab(index: number | undefined): Promise<string>; |
| 143 | + run( |
| 144 | + tool: Tool, |
| 145 | + params: Record<string, unknown> | undefined |
| 146 | + ): Promise<{ content: (ImageContent | TextContent)[] }>; |
| 147 | + waitForTimeout(time: number): Promise<void>; |
| 148 | + close(): Promise<void>; |
| 149 | +}; |
0 commit comments