-
Notifications
You must be signed in to change notification settings - Fork 4.7k
feat(mcp): add headers capability #37583
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lupuletic
wants to merge
4
commits into
microsoft:main
Choose a base branch
from
lupuletic:feature/mcp-headers
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+196
−3
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c82befe
feat(mcp): add headers capability
lupuletic f8c7317
chore: address review feedback on MCP headers
lupuletic 37ac01e
test: stabilize MCP headers suite across browsers
lupuletic 6ac2f2e
fix: guard MCP headers against whitespace names
lupuletic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* 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 { z } from '../../sdk/bundle'; | ||
import { defineTool } from './tool'; | ||
|
||
const setHeaders = defineTool({ | ||
capability: 'headers', | ||
|
||
schema: { | ||
name: 'browser_set_headers', | ||
title: 'Set extra HTTP headers', | ||
description: 'Persistently set custom HTTP headers on the active browser context.', | ||
inputSchema: z.object({ | ||
headers: z.record(z.string(), z.string()).describe('Header names mapped to the values that should be sent with every request.'), | ||
}), | ||
type: 'destructive', | ||
}, | ||
|
||
handle: async (context, params, response) => { | ||
const entries = Object.entries(params.headers); | ||
if (!entries.length) { | ||
response.addError('Please provide at least one header to set.'); | ||
return; | ||
} | ||
|
||
const invalidHeader = entries.find(([name]) => !name.trim()); | ||
if (invalidHeader) { | ||
response.addError('Header names must be non-empty strings.'); | ||
return; | ||
} | ||
|
||
await context.setExtraHTTPHeaders(params.headers); | ||
|
||
const count = entries.length; | ||
response.addResult(`Configured ${count} ${count === 1 ? 'header' : 'headers'} for this session.`); | ||
response.addCode(`await context.setExtraHTTPHeaders(${JSON.stringify(params.headers, null, 2)});`); | ||
}, | ||
}); | ||
|
||
export default [ | ||
setHeaders, | ||
]; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* 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 { test, expect } from './fixtures'; | ||
|
||
test('headers tool requires capability', async ({ client, startClient }) => { | ||
const { tools } = await client.listTools(); | ||
expect(tools.map(tool => tool.name)).not.toContain('browser_set_headers'); | ||
|
||
const { client: headersClient } = await startClient({ args: ['--caps=headers'] }); | ||
const headersToolList = await headersClient.listTools(); | ||
expect(headersToolList.tools.map(tool => tool.name)).toContain('browser_set_headers'); | ||
}); | ||
|
||
test('browser_set_headers rejects empty input', async ({ startClient }) => { | ||
const { client } = await startClient({ args: ['--caps=headers'] }); | ||
|
||
const response = await client.callTool({ | ||
name: 'browser_set_headers', | ||
arguments: { headers: {} }, | ||
}); | ||
|
||
expect(response).toHaveResponse({ | ||
isError: true, | ||
result: 'Please provide at least one header to set.', | ||
}); | ||
}); | ||
|
||
test('browser_set_headers rejects header names without characters', async ({ startClient }) => { | ||
const { client } = await startClient({ args: ['--caps=headers'] }); | ||
|
||
const response = await client.callTool({ | ||
name: 'browser_set_headers', | ||
arguments: { headers: { ' ': 'value' } }, | ||
}); | ||
|
||
expect(response).toHaveResponse({ | ||
isError: true, | ||
result: 'Header names must be non-empty strings.', | ||
}); | ||
}); | ||
|
||
test('browser_set_headers persists headers across navigations', async ({ startClient, server }) => { | ||
server.setContent('/first', '<title>First</title>', 'text/html'); | ||
server.setContent('/second', '<title>Second</title>', 'text/html'); | ||
|
||
const { client } = await startClient({ args: ['--caps=headers'] }); | ||
|
||
expect(await client.callTool({ | ||
name: 'browser_set_headers', | ||
arguments: { | ||
headers: { 'X-Tenant-ID': 'tenant-123' }, | ||
}, | ||
})).toHaveResponse({ | ||
result: 'Configured 1 header for this session.', | ||
}); | ||
|
||
const firstRequestPromise = server.waitForRequest('/first'); | ||
await client.callTool({ | ||
name: 'browser_navigate', | ||
arguments: { url: `${server.PREFIX}/first` }, | ||
}); | ||
const firstRequest = await firstRequestPromise; | ||
expect(firstRequest.headers['x-tenant-id']).toBe('tenant-123'); | ||
|
||
const secondRequestPromise = server.waitForRequest('/second'); | ||
await client.callTool({ | ||
name: 'browser_navigate', | ||
arguments: { url: `${server.PREFIX}/second` }, | ||
}); | ||
const secondRequest = await secondRequestPromise; | ||
expect(secondRequest.headers['x-tenant-id']).toBe('tenant-123'); | ||
}); | ||
|
||
test('browser_set_headers applies to all requests from the context', async ({ startClient, server }) => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: we already have it covered by the tests for context.setExtraHttpHeaders, I'd only test that the headers are sent at all. |
||
server.setRoute('/page', (req, res) => { | ||
res.writeHead(200, { 'Content-Type': 'text/html' }); | ||
res.end(`<!DOCTYPE html><script>fetch('/api/data')</script>`); | ||
}); | ||
server.setRoute('/api/data', (req, res) => { | ||
res.writeHead(200, { 'Content-Type': 'application/json' }); | ||
res.end('{}'); | ||
}); | ||
|
||
const { client } = await startClient({ args: ['--caps=headers'] }); | ||
|
||
expect(await client.callTool({ | ||
name: 'browser_set_headers', | ||
arguments: { | ||
headers: { | ||
'X-Tenant-ID': 'tenant-456', | ||
'Authorization': 'Bearer token456', | ||
}, | ||
}, | ||
})).toHaveResponse({ | ||
result: 'Configured 2 headers for this session.', | ||
}); | ||
|
||
const pageRequestPromise = server.waitForRequest('/page'); | ||
const apiRequestPromise = server.waitForRequest('/api/data'); | ||
|
||
await client.callTool({ | ||
name: 'browser_navigate', | ||
arguments: { url: `${server.PREFIX}/page` }, | ||
}); | ||
|
||
const [pageRequest, apiRequest] = await Promise.all([pageRequestPromise, apiRequestPromise]); | ||
|
||
expect(pageRequest.headers['x-tenant-id']).toBe('tenant-456'); | ||
expect(pageRequest.headers['authorization']).toBe('Bearer token456'); | ||
expect(apiRequest.headers['x-tenant-id']).toBe('tenant-456'); | ||
expect(apiRequest.headers['authorization']).toBe('Bearer token456'); | ||
}); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: ideally this check should be done in the setExtraHTTPHeaders implementation.