-
Notifications
You must be signed in to change notification settings - Fork 2k
Closed
Description
Hello Playwright Team,
I'm trying to run the programmatic case of playwright with express, but the server closes when inspected from MCPinspector.
Minimal reproducible example:
- Clone https://github.com/hydroweaver/playwright_mcp_programmatic
- npm i @modelcontextprotocol/sdk @playwright/mcp @types/express
- CD \playwright_mcp_programmatic\dist
- npx tsc
- node index.js
The server runs, but when inspection from MCP inspector is done, the console errors out at:
server.connect is not a function
Index.ts file (based on https://github.com/microsoft/playwright-mcp#:~:text=Programmatic%20usage,-Tools
import express, { Request, Response } from 'express';
import { randomUUID } from 'node:crypto';
import { createConnection } from '@playwright/mcp';
import { StreamableHTTPServerTransport } from '@modelcontextprotocol/sdk/server/streamableHttp.js';
const app = express();
app.use(express.json());
app.post('/mcp', async (req: Request, res: Response) => {
const server = await createConnection({ browser: { launchOptions: { headless: true } } });
try {
const transport: StreamableHTTPServerTransport = new StreamableHTTPServerTransport({
sessionIdGenerator: randomUUID,
});
await server.connect(transport);
await transport.handleRequest(req, res, req.body);
res.on('close', () => {
console.log('Request closed');
transport.close();
server.close();
});
} catch (error) {
console.error('Error handling MCP request:', error);
if (!res.headersSent) {
res.status(500).json({
jsonrpc: '2.0',
error: {
code: -32603,
message: 'Internal server error',
},
id: null,
});
}
}
});
// Start the server
const PORT = 8931;
app.listen(PORT, () => {
console.log(`MCP Stateless Streamable HTTP Server listening on port ${PORT}`);
});
// Handle server shutdown
process.on('SIGINT', async () => {
console.log('Shutting down server...');
process.exit(0);
});Metadata
Metadata
Assignees
Labels
No labels
