Skip to content
Merged
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
5 changes: 5 additions & 0 deletions .changeset/tender-experts-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@browserbasehq/stagehand": patch
---

Supports request header authentication with connectToMCPServer
14 changes: 12 additions & 2 deletions packages/core/lib/v3/mcp/connection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ import {
Client,
ClientOptions,
} from "@modelcontextprotocol/sdk/client/index.js";
import { StreamableHTTPClientTransport } from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import {
StreamableHTTPClientTransport,
type StreamableHTTPClientTransportOptions,
} from "@modelcontextprotocol/sdk/client/streamableHttp.js";
import { StdioClientTransport } from "@modelcontextprotocol/sdk/client/stdio.js";
import { MCPConnectionError } from "../types/public/sdkErrors.js";

export interface ConnectToMCPServerOptions {
serverUrl: string | URL;
clientOptions?: ClientOptions;
requestOptions?: StreamableHTTPClientTransportOptions;
}

export interface StdioServerConfig {
Expand All @@ -23,6 +27,7 @@ export const connectToMCPServer = async (
try {
let transport;
let clientOptions: ClientOptions | undefined;
let requestOptions: StreamableHTTPClientTransportOptions | undefined;

// Check if it's a stdio config (has 'command' property)
if (typeof serverConfig === "object" && "command" in serverConfig) {
Expand All @@ -37,9 +42,14 @@ export const connectToMCPServer = async (
serverUrl = (serverConfig as ConnectToMCPServerOptions).serverUrl;
clientOptions = (serverConfig as ConnectToMCPServerOptions)
.clientOptions;
requestOptions = (serverConfig as ConnectToMCPServerOptions)
.requestOptions;
}

transport = new StreamableHTTPClientTransport(new URL(serverUrl));
transport = new StreamableHTTPClientTransport(
new URL(serverUrl),
requestOptions,
);
}

const client = new Client({
Expand Down
6 changes: 5 additions & 1 deletion packages/core/tests/unit/public-api/v3-core.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,11 @@ describe("V3 Core public API types", () => {
| string
| URL
| { command: string; args?: string[]; env?: Record<string, string> }
| { serverUrl: string | URL; clientOptions?: unknown };
| {
serverUrl: string | URL;
clientOptions?: unknown;
requestOptions?: unknown;
};

it("has correct parameter types", () => {
expectTypeOf(
Expand Down
19 changes: 18 additions & 1 deletion packages/docs/v3/best-practices/mcp-integrations.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ const notionClient = await connectToMCPServer({
},
});

// Use the connected client
// Use the connected clients (example with Supabase + Notion)
const agent = stagehand.agent({
provider: "openai",
model: "computer-use-preview",
Expand All @@ -81,6 +81,23 @@ const agent = stagehand.agent({
await agent.execute("Search for restaurants in New Brunswick, NJ and save the first result to the database");
```

## Authenticated MCP Servers

Some MCP servers require authentication via HTTP request headers. You can pass request headers through `requestOptions`:

```typescript
const authenticatedClient = await connectToMCPServer({
serverUrl: "https://mcp-server.example.com/mcp",
requestOptions: {
requestInit: {
headers: {
Authorization: `Bearer ${process.env.MCP_SERVER_API_KEY}`,
},
},
},
});
```



## Multiple Integrations
Expand Down
Loading