Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
182 changes: 131 additions & 51 deletions browser-tools-mcp/mcp-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import path from "path";
import fs from "fs";
import { z } from "zod";

// Create the MCP server
const server = new McpServer({
Expand Down Expand Up @@ -175,78 +176,157 @@ async function withServerConnection<T>(
}

// We'll define our tools that retrieve data from the browser connector
server.tool("getConsoleLogs", "Check our browser logs", async () => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/console-logs`
);
const json = await response.json();
return {
content: [
{
type: "text",
text: JSON.stringify(json, null, 2),
},
],
};
});
});
server.tool(
"getConsoleLogs",
"Check our browser logs",
{
respKeywords: z.array(z.string()).describe("The keywords contained in the log message").optional(),
},
async ({ respKeywords }) => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/console-logs`
);
const json = await response.json();
/**
* json eg:
[
{
"type": "console-log",
"level": "log",
"message": "content script loaded",
"timestamp": 1762399170098
},
]
*/

let result: Array<any> = json;
if (respKeywords) {
result = result.filter(log => respKeywords.some(keyword => log.message.includes(keyword)));
}

return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
});
},
);

server.tool(
"getConsoleErrors",
"Check our browsers console errors",
async () => {
{
respKeywords: z.array(z.string()).describe("The keywords contained in the log message").optional(),
},
async ({ respKeywords }) => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/console-errors`
);
const json = await response.json();

let result: Array<any> = json;
if (respKeywords) {
result = result.filter(log => respKeywords.some(keyword => log.message.includes(keyword)));
}

return {
content: [
{
type: "text",
text: JSON.stringify(json, null, 2),
text: JSON.stringify(result, null, 2),
},
],
};
});
}
);

server.tool("getNetworkErrors", "Check our network ERROR logs", async () => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/network-errors`
);
const json = await response.json();
return {
content: [
{
type: "text",
text: JSON.stringify(json, null, 2),
},
],
isError: true,
};
});
});
server.tool(
"getNetworkErrors",
"Check our network ERROR logs",
{
urlKeywords: z.string().describe("The keywords contained in the request URL").optional(),
respKeywords: z.array(z.string()).describe("The keywords contained in the response body").optional(),
},
async ({ respKeywords, urlKeywords }) => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/network-errors`
);
const json = await response.json();
let result: Array<any> = json;
if (urlKeywords) {
result = result.filter(log => log.url.includes(urlKeywords));
}
if (respKeywords) {
result = result.filter(log => respKeywords.some(keyword => log.responseBody.includes(keyword)));
}

server.tool("getNetworkLogs", "Check ALL our network logs", async () => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/network-success`
);
const json = await response.json();
return {
content: [
{
type: "text",
text: JSON.stringify(json, null, 2),
},
],
};
});
});
return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
isError: true,
};
});
},
);

server.tool(
"getNetworkLogs",
"Check ALL our network logs",
{
urlKeywords: z.string().describe("The keywords contained in the request URL").optional(),
respKeywords: z.array(z.string()).describe("The keywords contained in the response body").optional(),
},
async ({ respKeywords, urlKeywords }) => {
return await withServerConnection(async () => {
const response = await fetch(
`http://${discoveredHost}:${discoveredPort}/network-success`
);
const json = await response.json();
/**
* json eg:
* [
{
"type": "network-request",
"url": "https://xxx",
"method": "GET",
"status": 200,
"requestBody": "",
"responseBody": "{}",
"timestamp": 1762344730925
}
* ]
*/
let result: Array<any> = json;
if (urlKeywords) {
result = result.filter(log => log.url.includes(urlKeywords));
}
if (respKeywords) {
result = result.filter(log => respKeywords.some(keyword => log.responseBody.includes(keyword)));
}

return {
content: [
{
type: "text",
text: JSON.stringify(result, null, 2),
},
],
};
});
},
);

server.tool(
"takeScreenshot",
Expand Down
4 changes: 2 additions & 2 deletions browser-tools-mcp/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.