playwright-mcp/src/connection.ts

86 lines
3.2 KiB
TypeScript
Raw Normal View History

2025-03-21 10:58:58 -07:00
/**
* 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 { Server } from '@modelcontextprotocol/sdk/server/index.js';
import { CallToolRequestSchema, ListToolsRequestSchema, Tool as McpTool } from '@modelcontextprotocol/sdk/types.js';
import { zodToJsonSchema } from 'zod-to-json-schema';
import { Context } from './context.js';
2025-07-22 16:36:21 -07:00
import { Response } from './response.js';
import { allTools } from './tools.js';
import { packageJSON } from './package.js';
2025-07-14 10:52:38 -07:00
import { FullConfig } from './config.js';
2025-07-22 20:06:03 -07:00
import { SessionLog } from './sessionLog.js';
import { logUnhandledError } from './log.js';
import type { BrowserContextFactory } from './browserContextFactory.js';
export async function createMCPServer(config: FullConfig, browserContextFactory: BrowserContextFactory): Promise<Server> {
const tools = allTools.filter(tool => tool.capability.startsWith('core') || config.capabilities?.includes(tool.capability));
const context = new Context(tools, config, browserContextFactory);
const server = new Server({ name: 'Playwright', version: packageJSON.version }, {
capabilities: {
tools: {},
}
});
2025-03-21 10:58:58 -07:00
2025-07-22 20:06:03 -07:00
const sessionLog = config.saveSession ? await SessionLog.create(config) : undefined;
server.setRequestHandler(ListToolsRequestSchema, async () => {
return {
tools: tools.map(tool => ({
name: tool.schema.name,
description: tool.schema.description,
inputSchema: zodToJsonSchema(tool.schema.inputSchema),
annotations: {
title: tool.schema.title,
readOnlyHint: tool.schema.type === 'readOnly',
destructiveHint: tool.schema.type === 'destructive',
openWorldHint: true,
},
})) as McpTool[],
};
});
2025-03-21 10:58:58 -07:00
server.setRequestHandler(CallToolRequestSchema, async request => {
const errorResult = (...messages: string[]) => ({
content: [{ type: 'text', text: messages.join('\n') }],
isError: true,
});
const tool = tools.find(tool => tool.schema.name === request.params.name);
if (!tool)
return errorResult(`Tool "${request.params.name}" not found`);
try {
2025-07-22 20:06:03 -07:00
const response = new Response(context, request.params.name, request.params.arguments || {});
2025-07-22 16:36:21 -07:00
await tool.handle(context, tool.schema.inputSchema.parse(request.params.arguments || {}), response);
2025-07-22 20:06:03 -07:00
if (sessionLog)
await sessionLog.log(response);
2025-07-22 16:36:21 -07:00
return await response.serialize();
} catch (error) {
return errorResult(String(error));
}
});
2025-03-21 10:58:58 -07:00
server.oninitialized = () => {
context.clientVersion = server.getClientVersion();
};
server.onclose = () => {
void context.dispose().catch(logUnhandledError);
};
return server;
}