Skip to content

Commit 3f11b6e

Browse files
author
Mats Holmqvist
committed
Adding cloudflare header to all discovery calls modelcontextprotocol#1
1 parent f18775a commit 3f11b6e

5 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎.env‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Client (Vite)
2+
VITE_MCP_WELL_KNOWN_PREFIX=/auth/realms/cmem
3+
4+
# Server (Node proxy)
5+
CF_ACCESS_HEADER_NAME=cf-access-token
6+
CF_ACCESS_TOKEN=eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjZiOTA0ZTA1ZmU5MjEwZWU2MTRiYzdkMjNmNWFmNjNkZWY5YWI0ZTU2MGRiZTQ4MjU5N2IwODc0NjM4Nzg5ODUifQ.eyJ0eXBlIjoiYXBwIiwiaWF0IjoxNzc5MzY2MzYwLCJleHAiOjE3Nzk0NTI3NjEsImlzcyI6Imh0dHBzOi8vZXJpY3Nzb24uY2xvdWRmbGFyZWFjY2Vzcy5jb20iLCJzdWIiOiIiLCJhdWQiOiI5NDNiZTAwZjYxOThkZTY3Nzg2ZWM5ZTU1MDdiMGNmZGUxNzIxMjljMmYxMTU4Yzk4ZjFlY2EwODg4NzY0YzE4IiwiY29tbW9uX25hbWUiOiJmODkzOWNlOWQ3Y2RiZGE5YzIzYjFlNTkyOGU4MzlhMy5hY2Nlc3MifQ.Hn0GMFyw97kgBudZrDvpgpnsYmWgoEHgEePh4e8t8p8xZwBBxjd9ZlHtiWThjlI1594vaDU63ZQdl3Hx8EqKvFdQQDnv3Q0moP5BHAyomBCA3xsJ6oNrUnanPlW5v9V66Un0mQ_ZnQQXJcjhLGKs44mlnJzQI_wE6KNXfn4U6D7boakcwdO0JKddGh5O2jkaHk0Om301VcUYZ8btL7uBLMo0D2624YwkMn15n3-8A3UKe3ifQ1ZQ6hlre0E6M46ZDi8Jk4YeZFWHMCy8lQWLRyUmD0SCXS6HzWAyAMtZpd-E1ZmCPfWr3gIN_u2dfr5yUt0dEn04KmLlBBXazjAkDw

‎client/src/utils/oauthUtils.ts‎

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,17 @@ export const getAuthorizationServerMetadataDiscoveryUrl = (
9999
typeof authorizationServerUrl === "string"
100100
? new URL(authorizationServerUrl)
101101
: authorizationServerUrl;
102+
103+
// Optional prefix before /.well-known (e.g. "/cf-proxy" when going through Cloudflare)
104+
const prefix = import.meta.env.VITE_MCP_WELL_KNOWN_PREFIX ?? "";
105+
102106
const hasPath = url.pathname !== "/";
103107

104108
if (!hasPath) {
105-
return new URL("/.well-known/oauth-authorization-server", url.origin).href;
109+
return new URL(
110+
`${prefix}/.well-known/oauth-authorization-server`,
111+
url.origin,
112+
).href;
106113
}
107114

108115
// Strip trailing slash to avoid double slashes in tenant-aware discovery URLs.
@@ -111,7 +118,37 @@ export const getAuthorizationServerMetadataDiscoveryUrl = (
111118
: url.pathname;
112119

113120
return new URL(
114-
`/.well-known/oauth-authorization-server${pathname}`,
121+
`${prefix}${pathname}/.well-known/oauth-authorization-server`,
122+
url.origin,
123+
).href;
124+
};
125+
126+
/**
127+
* Returns the OAuth protected resource metadata discovery URL
128+
* for a given MCP server URL, including path and optional prefix.
129+
*/
130+
export const getResourceMetadataDiscoveryUrl = (
131+
serverUrl: string | URL,
132+
): string => {
133+
const url = typeof serverUrl === "string" ? new URL(serverUrl) : serverUrl;
134+
135+
const prefix = import.meta.env.VITE_MCP_WELL_KNOWN_PREFIX ?? "";
136+
137+
const hasPath = url.pathname !== "/";
138+
if (!hasPath) {
139+
return new URL(`${prefix}/.well-known/oauth-protected-resource`, url.origin)
140+
.href;
141+
}
142+
143+
// Strip trailing slash so we don't end up with double slashes.
144+
const pathname = url.pathname.endsWith("/")
145+
? url.pathname.slice(0, -1)
146+
: url.pathname;
147+
148+
// Path-aware variant per RFC 9728:
149+
// {origin}{prefix}/.well-known/oauth-protected-resource{pathname}
150+
return new URL(
151+
`${prefix}${pathname}/.well-known/oauth-protected-resource`,
115152
url.origin,
116153
).href;
117154
};

‎package-lock.json‎

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"@modelcontextprotocol/inspector-server": "^0.21.2",
5858
"@modelcontextprotocol/sdk": "^1.25.2",
5959
"concurrently": "^9.2.0",
60+
"dotenv": "^17.4.2",
6061
"node-fetch": "^3.3.2",
6162
"open": "^10.2.0",
6263
"shell-quote": "^1.8.3",

‎server/src/index.ts‎

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,31 @@
11
#!/usr/bin/env node
2-
2+
import dotenv from "dotenv";
33
import cors from "cors";
44
import { parseArgs } from "node:util";
55
import { parse as shellParseArgs } from "shell-quote";
66
import nodeFetch, { Headers as NodeHeaders } from "node-fetch";
77

8+
import { resolve as resolvePath } from "path";
9+
10+
const __filename = fileURLToPath(import.meta.url);
11+
const __dirname = dirname(__filename);
12+
13+
// Adjust the path if your .env is at the repo root (C:\dev\mcp-inspector.env)
14+
dotenv.config({
15+
path: resolvePath(__dirname, "..", "..", ".env"),
16+
});
17+
818
// Type-compatible wrappers for node-fetch to work with browser-style types
919
const fetch = nodeFetch;
1020
const Headers = NodeHeaders;
1121

22+
// Cloudflare Access header config:
23+
// - CF_ACCESS_TOKEN: the token value
24+
// - CF_ACCESS_HEADER_NAME: header name, defaults to "cf-access-token"
25+
const CF_ACCESS_TOKEN = process.env.CF_ACCESS_TOKEN; //|| "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6IjZiOTA0ZTA1ZmU5MjEwZWU2MTRiYzdkMjNmNWFmNjNkZWY5YWI0ZTU2MGRiZTQ4MjU5N2IwODc0NjM4Nzg5ODUifQ.eyJ0eXBlIjoiYXBwIiwiaWF0IjoxNzc5MzY2MzYwLCJleHAiOjE3Nzk0NTI3NjEsImlzcyI6Imh0dHBzOi8vZXJpY3Nzb24uY2xvdWRmbGFyZWFjY2Vzcy5jb20iLCJzdWIiOiIiLCJhdWQiOiI5NDNiZTAwZjYxOThkZTY3Nzg2ZWM5ZTU1MDdiMGNmZGUxNzIxMjljMmYxMTU4Yzk4ZjFlY2EwODg4NzY0YzE4IiwiY29tbW9uX25hbWUiOiJmODkzOWNlOWQ3Y2RiZGE5YzIzYjFlNTkyOGU4MzlhMy5hY2Nlc3MifQ.Hn0GMFyw97kgBudZrDvpgpnsYmWgoEHgEePh4e8t8p8xZwBBxjd9ZlHtiWThjlI1594vaDU63ZQdl3Hx8EqKvFdQQDnv3Q0moP5BHAyomBCA3xsJ6oNrUnanPlW5v9V66Un0mQ_ZnQQXJcjhLGKs44mlnJzQI_wE6KNXfn4U6D7boakcwdO0JKddGh5O2jkaHk0Om301VcUYZ8btL7uBLMo0D2624YwkMn15n3-8A3UKe3ifQ1ZQ6hlre0E6M46ZDi8Jk4YeZFWHMCy8lQWLRyUmD0SCXS6HzWAyAMtZpd-E1ZmCPfWr3gIN_u2dfr5yUt0dEn04KmLlBBXazjAkDw"
26+
const CF_ACCESS_HEADER_NAME =
27+
process.env.CF_ACCESS_HEADER_NAME || "cf-access-token";
28+
1229
import {
1330
SSEClientTransport,
1431
SseError,
@@ -361,6 +378,11 @@ const createCustomFetch = (headerHolder: ProxyHeaderHolder) => {
361378
finalHeaders.set(key, value);
362379
});
363380

381+
// Inject Cloudflare Access header if configured
382+
if (CF_ACCESS_TOKEN) {
383+
finalHeaders.set(CF_ACCESS_HEADER_NAME, CF_ACCESS_TOKEN);
384+
}
385+
364386
// Convert Headers to a plain object for node-fetch compatibility
365387
const headersObject: Record<string, string> = {};
366388
finalHeaders.forEach((value, key) => {
@@ -888,9 +910,18 @@ app.post(
888910
return;
889911
}
890912

913+
const headersInit: Record<string, string> = {
914+
...(init?.headers as Record<string, string>),
915+
};
916+
917+
// Inject Cloudflare Access header here as well (used by /fetch helper)
918+
if (CF_ACCESS_TOKEN) {
919+
headersInit[CF_ACCESS_HEADER_NAME] = CF_ACCESS_TOKEN;
920+
}
921+
891922
const response = await fetch(url, {
892923
method: init?.method ?? "GET",
893-
headers: (init?.headers as Record<string, string>) ?? {},
924+
headers: headersInit,
894925
body: init?.body as string | undefined,
895926
});
896927

0 commit comments

Comments
 (0)