Skip to content

Commit a58a259

Browse files
committed
Merge remote-tracking branch 'upstream/main'
2 parents 1e6063b + 7df0ac4 commit a58a259

File tree

3 files changed

+117
-33
lines changed

3 files changed

+117
-33
lines changed

README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,31 @@ The MCP inspector is a developer tool for testing and debugging MCP servers.
44

55
![MCP Inspector Screenshot](https://raw.githubusercontent.com/modelcontextprotocol/inspector/main/mcp-inspector.png)
66

7+
## Architecture Overview
8+
9+
The MCP Inspector consists of two main components that work together:
10+
11+
- **MCP Inspector Client (MCPI)**: A React-based web UI that provides an interactive interface for testing and debugging MCP servers
12+
- **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)
13+
14+
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.
15+
716
## Running the Inspector
817

918
### Requirements
1019

1120
- Node.js: ^22.7.5
1221

22+
### Quick Start (UI mode)
23+
24+
To get up and running right away with the UI, just execute the following:
25+
26+
```bash
27+
npx @modelcontextprotocol/inspector
28+
```
29+
30+
The server will start up and the UI will be accessible at `http://localhost:6274`.
31+
1332
### From an MCP server repository
1433

1534
To inspect an MCP server implementation, there's no need to clone this repo. Instead, use `npx`. For example, if your server is built at `build/index.js`:

client/src/lib/hooks/useConnection.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import {
4545
} from "@/utils/configUtils";
4646
import { getMCPServerRequestTimeout } from "@/utils/configUtils";
4747
import { InspectorConfig } from "../configurationTypes";
48+
import { Transport } from "@modelcontextprotocol/sdk/shared/transport.js";
4849

4950
interface UseConnectionOptions {
5051
transportType: "stdio" | "sse" | "streamable-http";
@@ -83,6 +84,9 @@ export function useConnection({
8384
const [serverCapabilities, setServerCapabilities] =
8485
useState<ServerCapabilities | null>(null);
8586
const [mcpClient, setMcpClient] = useState<Client | null>(null);
87+
const [clientTransport, setClientTransport] = useState<Transport | null>(
88+
null,
89+
);
8690
const [requestHistory, setRequestHistory] = useState<
8791
{ request: string; response?: string }[]
8892
>([]);
@@ -304,7 +308,14 @@ export function useConnection({
304308
bearerToken || (await serverAuthProvider.tokens())?.access_token;
305309
if (token) {
306310
const authHeaderName = headerName || "Authorization";
307-
headers[authHeaderName] = `Bearer ${token}`;
311+
312+
// Add custom header name as a special request header to let the server know which header to pass through
313+
if (authHeaderName.toLowerCase() !== "authorization") {
314+
headers[authHeaderName] = token;
315+
headers["x-custom-auth-header"] = authHeaderName;
316+
} else {
317+
headers[authHeaderName] = `Bearer ${token}`;
318+
}
308319
}
309320

310321
// Create appropriate transport
@@ -377,14 +388,6 @@ export function useConnection({
377388
transportType,
378389
);
379390

380-
const clientTransport =
381-
transportType === "streamable-http"
382-
? new StreamableHTTPClientTransport(mcpProxyServerUrl as URL, {
383-
sessionId: undefined,
384-
...transportOptions,
385-
})
386-
: new SSEClientTransport(mcpProxyServerUrl as URL, transportOptions);
387-
388391
if (onNotification) {
389392
[
390393
CancelledNotificationSchema,
@@ -414,7 +417,20 @@ export function useConnection({
414417

415418
let capabilities;
416419
try {
417-
await client.connect(clientTransport);
420+
const transport =
421+
transportType === "streamable-http"
422+
? new StreamableHTTPClientTransport(mcpProxyServerUrl as URL, {
423+
sessionId: undefined,
424+
...transportOptions,
425+
})
426+
: new SSEClientTransport(
427+
mcpProxyServerUrl as URL,
428+
transportOptions,
429+
);
430+
431+
await client.connect(transport as Transport);
432+
433+
setClientTransport(transport);
418434

419435
capabilities = client.getServerCapabilities();
420436
const initializeRequest = {
@@ -468,10 +484,15 @@ export function useConnection({
468484
};
469485

470486
const disconnect = async () => {
487+
if (transportType === "streamable-http")
488+
await (
489+
clientTransport as StreamableHTTPClientTransport
490+
).terminateSession();
471491
await mcpClient?.close();
472492
const authProvider = new InspectorOAuthClientProvider(sseUrl);
473493
authProvider.clear();
474494
setMcpClient(null);
495+
setClientTransport(null);
475496
setConnectionStatus("disconnected");
476497
setCompletionsSupported(false);
477498
setServerCapabilities(null);

server/src/index.ts

Lines changed: 67 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,44 @@ const { values } = parseArgs({
4141
},
4242
});
4343

44+
// Function to get HTTP headers.
45+
// Supports only "sse" and "streamable-http" transport types.
46+
const getHttpHeaders = (
47+
req: express.Request,
48+
transportType: string,
49+
): HeadersInit => {
50+
const headers: HeadersInit = {
51+
Accept:
52+
transportType === "sse"
53+
? "text/event-stream"
54+
: "text/event-stream, application/json",
55+
};
56+
const defaultHeaders =
57+
transportType === "sse"
58+
? SSE_HEADERS_PASSTHROUGH
59+
: STREAMABLE_HTTP_HEADERS_PASSTHROUGH;
60+
61+
for (const key of defaultHeaders) {
62+
if (req.headers[key] === undefined) {
63+
continue;
64+
}
65+
66+
const value = req.headers[key];
67+
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
68+
}
69+
70+
// If the header "x-custom-auth-header" is present, use its value as the custom header name.
71+
if (req.headers["x-custom-auth-header"] !== undefined) {
72+
const customHeaderName = req.headers["x-custom-auth-header"] as string;
73+
const lowerCaseHeaderName = customHeaderName.toLowerCase();
74+
if (req.headers[lowerCaseHeaderName] !== undefined) {
75+
const value = req.headers[lowerCaseHeaderName];
76+
headers[customHeaderName] = value as string;
77+
}
78+
}
79+
return headers;
80+
};
81+
4482
const app = express();
4583
app.use(cors());
4684
app.use((req, res, next) => {
@@ -80,18 +118,8 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
80118
return transport;
81119
} else if (transportType === "sse") {
82120
const url = query.url as string;
83-
const headers: HeadersInit = {
84-
Accept: "text/event-stream",
85-
};
86121

87-
for (const key of SSE_HEADERS_PASSTHROUGH) {
88-
if (req.headers[key] === undefined) {
89-
continue;
90-
}
91-
92-
const value = req.headers[key];
93-
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
94-
}
122+
const headers = getHttpHeaders(req, transportType);
95123

96124
console.log(`SSE transport: url=${url}, headers=${Object.keys(headers)}`);
97125

@@ -108,18 +136,7 @@ const createTransport = async (req: express.Request): Promise<Transport> => {
108136
console.log("Connected to SSE transport");
109137
return transport;
110138
} else if (transportType === "streamable-http") {
111-
const headers: HeadersInit = {
112-
Accept: "text/event-stream, application/json",
113-
};
114-
115-
for (const key of STREAMABLE_HTTP_HEADERS_PASSTHROUGH) {
116-
if (req.headers[key] === undefined) {
117-
continue;
118-
}
119-
120-
const value = req.headers[key];
121-
headers[key] = Array.isArray(value) ? value[value.length - 1] : value;
122-
}
139+
const headers = getHttpHeaders(req, transportType);
123140

124141
const transport = new StreamableHTTPClientTransport(
125142
new URL(query.url as string),
@@ -226,6 +243,33 @@ app.post("/mcp", async (req, res) => {
226243
}
227244
});
228245

246+
app.delete("/mcp", async (req, res) => {
247+
const sessionId = req.headers["mcp-session-id"] as string | undefined;
248+
console.log(`Received DELETE message for sessionId ${sessionId}`);
249+
let serverTransport: Transport | undefined;
250+
if (sessionId) {
251+
try {
252+
serverTransport = serverTransports.get(
253+
sessionId,
254+
) as StreamableHTTPClientTransport;
255+
if (!serverTransport) {
256+
res.status(404).end("Transport not found for sessionId " + sessionId);
257+
} else {
258+
await (
259+
serverTransport as StreamableHTTPClientTransport
260+
).terminateSession();
261+
webAppTransports.delete(sessionId);
262+
serverTransports.delete(sessionId);
263+
console.log(`Transports removed for sessionId ${sessionId}`);
264+
}
265+
res.status(200).end();
266+
} catch (error) {
267+
console.error("Error in /mcp route:", error);
268+
res.status(500).json(error);
269+
}
270+
}
271+
});
272+
229273
app.get("/stdio", async (req, res) => {
230274
try {
231275
console.log("New connection");

0 commit comments

Comments
 (0)