Merge branch 'main' into normalize-connection-logging

This commit is contained in:
Cliff Hall 2025-06-02 10:25:30 -04:00 • committed by GitHub
commit 789c90cc0b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 57 additions and 24 deletions

View file

@ -4,6 +4,15 @@ The MCP inspector is a developer tool for testing and debugging MCP servers.
![MCP Inspector Screenshot](https://raw.githubusercontent.com/modelcontextprotocol/inspector/main/mcp-inspector.png) ![MCP Inspector Screenshot](https://raw.githubusercontent.com/modelcontextprotocol/inspector/main/mcp-inspector.png)
## Architecture Overview
The MCP Inspector consists of two main components that work together:
- **MCP Inspector Client (MCPI)**: A React-based web UI that provides an interactive interface for testing and debugging MCP servers
- **MCP Proxy (MCPP)**: A Node.js server that acts as a protocol bridge, connecting the web UI to MCP servers via various transport methods (stdio, SSE, streamable-http)
Note that the proxy is not a network proxy for intercepting traffic. Instead, it functions as both an MCP client (connecting to your MCP server) and an HTTP server (serving the web UI), enabling browser-based interaction with MCP servers that use different transport protocols.
## Running the Inspector ## Running the Inspector
### Requirements ### Requirements

View file

@ -308,7 +308,14 @@ export function useConnection({
bearerToken || (await serverAuthProvider.tokens())?.access_token; bearerToken || (await serverAuthProvider.tokens())?.access_token;
if (token) { if (token) {
const authHeaderName = headerName || "Authorization"; const authHeaderName = headerName || "Authorization";
headers[authHeaderName] = `Bearer ${token}`;
// Add custom header name as a special request header to let the server know which header to pass through
if (authHeaderName.toLowerCase() !== "authorization") {
headers[authHeaderName] = token;
headers["x-custom-auth-header"] = authHeaderName;
} else {
headers[authHeaderName] = `Bearer ${token}`;
}
} }
// Create appropriate transport // Create appropriate transport

View file

@ -41,6 +41,44 @@ const { values } = parseArgs({
}, },
}); });
// Function to get HTTP headers.
// Supports only "sse" and "streamable-http" transport types.
const getHttpHeaders = (
req: express.Request,
transportType: string,
): HeadersInit => {
const headers: HeadersInit = {
Accept:
transportType === "sse"
? "text/event-stream"
: "text/event-stream, application/json",
};
const defaultHeaders =
transportType === "sse"
? SSE_HEADERS_PASSTHROUGH
: STREAMABLE_HTTP_HEADERS_PASSTHROUGH;
for (const key of defaultHeaders) {
if (req.headers[key] === undefined) {
continue;
}
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
if (req.headers["x-custom-auth-header"] !== undefined) {
const customHeaderName = req.headers["x-custom-auth-header"] as string;
const lowerCaseHeaderName = customHeaderName.toLowerCase();
if (req.headers[lowerCaseHeaderName] !== undefined) {
const value = req.headers[lowerCaseHeaderName];
headers[customHeaderName] = value as string;
}
}
return headers;
};
const app = express(); const app = express();
app.use(cors()); app.use(cors());
app.use((req, res, next) => { app.use((req, res, next) => {
@ -78,18 +116,8 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
return transport; return transport;
} else if (transportType === "sse") { } else if (transportType === "sse") {
const url = query.url as string; const url = query.url as string;
const headers: HeadersInit = {
Accept: "text/event-stream",
};
for (const key of SSE_HEADERS_PASSTHROUGH) { const headers = getHttpHeaders(req, transportType);
if (req.headers[key] === undefined) {
continue;
}
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`); console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
@ -104,18 +132,7 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
await transport.start(); await transport.start();
return transport; return transport;
} else if (transportType === "streamable-http") { } else if (transportType === "streamable-http") {
const headers: HeadersInit = { const headers = getHttpHeaders(req, transportType);
Accept: "text/event-stream, application/json",
};
for (const key of STREAMABLE_HTTP_HEADERS_PASSTHROUGH) {
if (req.headers[key] === undefined) {
continue;
}
const value = req.headers[key];
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
}
const transport = new StreamableHTTPClientTransport( const transport = new StreamableHTTPClientTransport(
new URL(query.url as string), new URL(query.url as string),