-
Notifications
You must be signed in to change notification settings - Fork 358
Expand file tree
/
Copy pathindex.ts
More file actions
executable file
·201 lines (185 loc) · 5.75 KB
/
index.ts
File metadata and controls
executable file
·201 lines (185 loc) · 5.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
import * as dotenv from "dotenv";
dotenv.config();
import { randomUUID } from "crypto";
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { z } from "zod";
import type { MCPToolsArray } from "./types/types.js";
import { Context } from "./context.js";
import type { Config } from "../config.d.ts";
import { TOOLS } from "./tools/index.js";
import { RESOURCE_TEMPLATES } from "./mcp/resources.js";
import {
ListResourcesRequestSchema,
ReadResourceRequestSchema,
ListResourceTemplatesRequestSchema,
} from "@modelcontextprotocol/sdk/types.js";
// Configuration schema for Smithery - matches existing Config interface
export const configSchema = z
.object({
browserbaseApiKey: z.string().describe("The Browserbase API Key to use"),
browserbaseProjectId: z
.string()
.describe("The Browserbase Project ID to use"),
proxies: z
.boolean()
.optional()
.describe("Whether or not to use Browserbase proxies"),
advancedStealth: z
.boolean()
.optional()
.describe(
"Use advanced stealth mode. Only available to Browserbase Scale Plan users",
),
keepAlive: z
.boolean()
.optional()
.describe("Whether or not to keep the Browserbase session alive"),
context: z
.object({
contextId: z
.string()
.optional()
.describe("The ID of the context to use"),
persist: z
.boolean()
.optional()
.describe("Whether or not to persist the context"),
})
.optional(),
viewPort: z
.object({
browserWidth: z
.number()
.optional()
.describe("The width of the browser"),
browserHeight: z
.number()
.optional()
.describe("The height of the browser"),
})
.optional(),
server: z
.object({
port: z
.number()
.optional()
.describe("The port to listen on for SHTTP or MCP transport"),
host: z
.string()
.optional()
.describe(
"The host to bind the server to. Default is localhost. Use 0.0.0.0 to bind to all interfaces",
),
})
.optional(),
modelName: z
.string()
.optional()
.describe("The model to use for Stagehand (default: gemini-2.0-flash)"),
modelApiKey: z
.string()
.optional()
.describe(
"API key for the custom model provider. Required when using a model other than the default gemini-2.0-flash",
),
experimental: z
.boolean()
.optional()
.describe("Enable experimental Stagehand features"),
})
.refine(
(data) => {
// If a non-default model is explicitly specified, API key is required
if (data.modelName && data.modelName !== "gemini-2.0-flash") {
return (
data.modelApiKey !== undefined &&
typeof data.modelApiKey === "string" &&
data.modelApiKey.length > 0
);
}
return true;
},
{
message: "modelApiKey is required when specifying a custom model",
path: ["modelApiKey"],
},
);
// Default function for Smithery
export default function ({ config }: { config: z.infer<typeof configSchema> }) {
if (!config.browserbaseApiKey) {
throw new Error("browserbaseApiKey is required");
}
if (!config.browserbaseProjectId) {
throw new Error("browserbaseProjectId is required");
}
const server = new McpServer({
name: "Browserbase MCP Server",
version: "2.3.0",
description:
"Cloud browser automation server powered by Browserbase and Stagehand. Enables LLMs to navigate websites, interact with elements, extract data, and capture screenshots using natural language commands.",
capabilities: {
resources: {
subscribe: true,
listChanged: true,
},
tools: {},
},
});
const internalConfig: Config = config as Config;
// Create the context, passing server instance and config
const contextId = randomUUID();
const context = new Context(server.server, internalConfig, contextId);
server.server.registerCapabilities({
resources: {
subscribe: true,
listChanged: true,
},
});
// Add resource handlers
server.server.setRequestHandler(ListResourcesRequestSchema, async () => {
return context.listResources();
});
server.server.setRequestHandler(
ReadResourceRequestSchema,
async (request) => {
return context.readResource(request.params.uri);
},
);
server.server.setRequestHandler(
ListResourceTemplatesRequestSchema,
async () => {
return { resourceTemplates: RESOURCE_TEMPLATES };
},
);
const tools: MCPToolsArray = [...TOOLS];
// Register each tool with the Smithery server
tools.forEach((tool) => {
if (tool.schema.inputSchema instanceof z.ZodObject) {
server.tool(
tool.schema.name,
tool.schema.description,
tool.schema.inputSchema.shape,
async (params: z.infer<typeof tool.schema.inputSchema>) => {
try {
const result = await context.run(tool, params);
return result;
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error);
process.stderr.write(
`[Smithery Error] ${new Date().toISOString()} Error running tool ${tool.schema.name}: ${errorMessage}\n`,
);
throw new Error(
`Failed to run tool '${tool.schema.name}': ${errorMessage}`,
);
}
},
);
} else {
console.warn(
`Tool "${tool.schema.name}" has an input schema that is not a ZodObject. Schema type: ${tool.schema.inputSchema.constructor.name}`,
);
}
});
return server.server;
}