Skip to content

Commit d92cefb

Browse files
committed
refactor(config): cleaned up base url logic
1 parent d2d1c67 commit d92cefb

File tree

2 files changed

+24
-17
lines changed

2 files changed

+24
-17
lines changed

src/config.ts

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ const configSchema = z.object({
2121
OAUTH_SCOPE: z.string().default("openid profile email"),
2222
});
2323

24-
export type Config = z.infer<typeof configSchema>;
24+
export type Config = z.infer<typeof configSchema> & {
25+
BASE_URL: string;
26+
};
2527

2628
let config: Config;
2729

@@ -30,19 +32,25 @@ export function getConfig(): Config {
3032
try {
3133
const parsed = configSchema.parse(process.env);
3234

35+
if (!parsed.BASE_URL) {
36+
parsed.BASE_URL = `http://localhost:${parsed.PORT}`;
37+
}
38+
3339
// Full mode validation - OAuth Authorization Server with external IdP
3440
if (parsed.AUTH_MODE === "full") {
3541
const requiredVars = [];
3642
if (!parsed.OAUTH_ISSUER) requiredVars.push("OAUTH_ISSUER");
3743
if (!parsed.OAUTH_CLIENT_ID) requiredVars.push("OAUTH_CLIENT_ID");
38-
if (!parsed.OAUTH_CLIENT_SECRET) requiredVars.push("OAUTH_CLIENT_SECRET");
44+
if (!parsed.OAUTH_CLIENT_SECRET)
45+
requiredVars.push("OAUTH_CLIENT_SECRET");
3946

4047
// Provide default for OAUTH_REDIRECT_URI if not set
4148
if (!parsed.OAUTH_REDIRECT_URI) {
42-
const baseUrl = parsed.BASE_URL || "http://localhost:3000";
43-
const callbackUrl = new URL("/callback", baseUrl);
49+
const callbackUrl = new URL("/callback", parsed.BASE_URL);
4450
parsed.OAUTH_REDIRECT_URI = callbackUrl.toString();
45-
console.log(`⚠️ OAUTH_REDIRECT_URI not set, using default: ${parsed.OAUTH_REDIRECT_URI}`);
51+
console.log(
52+
`⚠️ OAUTH_REDIRECT_URI not set, using default: ${parsed.OAUTH_REDIRECT_URI}`,
53+
);
4654
}
4755

4856
if (requiredVars.length > 0) {
@@ -52,21 +60,20 @@ export function getConfig(): Config {
5260
"OAUTH_ISSUER=https://your-domain.auth0.com\n" +
5361
"OAUTH_CLIENT_ID=your-client-id\n" +
5462
"OAUTH_CLIENT_SECRET=your-client-secret\n" +
55-
"OAUTH_REDIRECT_URI=http://localhost:3000/callback # Optional, defaults to BASE_URL/callback\n" +
56-
"OAUTH_AUDIENCE=your-api-identifier # Optional but recommended"
63+
"OAUTH_REDIRECT_URI=http://localhost:3000/callback # Optional, defaults to BASE_URL/callback or http://localhost:PORT/callback\n" +
64+
"OAUTH_AUDIENCE=your-api-identifier # Optional but recommended",
5765
);
5866
}
5967

60-
// OAUTH_AUDIENCE is optional but recommended for full mode
68+
// OAUTH_AUDIENCE is optional but recommended when a resource server is used
6169
if (!parsed.OAUTH_AUDIENCE) {
6270
console.warn(
6371
"⚠️ OAUTH_AUDIENCE not set for full mode. Token validation will not check audience.\n" +
64-
" For production deployments, consider setting OAUTH_AUDIENCE to your API identifier"
72+
" For production deployments, consider setting OAUTH_AUDIENCE to your API identifier",
6573
);
6674
}
6775
}
6876

69-
// Resource server mode validation
7077
if (parsed.AUTH_MODE === "resource_server") {
7178
const requiredVars = [];
7279
if (!parsed.OAUTH_ISSUER) requiredVars.push("OAUTH_ISSUER");
@@ -77,12 +84,12 @@ export function getConfig(): Config {
7784
`AUTH_MODE=resource_server requires OAuth configuration. Missing: ${requiredVars.join(", ")}\n` +
7885
"Example configuration:\n" +
7986
"OAUTH_ISSUER=https://your-domain.auth0.com\n" +
80-
"OAUTH_AUDIENCE=your-api-identifier"
87+
"OAUTH_AUDIENCE=your-api-identifier",
8188
);
8289
}
8390
}
8491

85-
config = parsed;
92+
config = parsed as Config;
8693
} catch (error) {
8794
console.error("❌ Invalid environment configuration:", error);
8895
process.exit(1);

src/index.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,10 +113,10 @@ const mcpHandler = async (req: express.Request, res: express.Response) => {
113113
capabilities,
114114
...(config.AUTH_MODE !== "none" && {
115115
oauth: {
116-
authorization_server: new URL("/.well-known/oauth-authorization-server", config.BASE_URL || "http://localhost:3000").toString(),
117-
protected_resource: new URL("/.well-known/oauth-protected-resource", config.BASE_URL || "http://localhost:3000").toString(),
118-
authorization_endpoint: new URL("/oauth/authorize", config.BASE_URL || "http://localhost:3000").toString(),
119-
token_endpoint: new URL("/oauth/token", config.BASE_URL || "http://localhost:3000").toString()
116+
authorization_server: new URL("/.well-known/oauth-authorization-server", config.BASE_URL).toString(),
117+
protected_resource: new URL("/.well-known/oauth-protected-resource", config.BASE_URL).toString(),
118+
authorization_endpoint: new URL("/oauth/authorize", config.BASE_URL).toString(),
119+
token_endpoint: new URL("/oauth/token", config.BASE_URL).toString()
120120
}
121121
})
122122
});
@@ -133,7 +133,7 @@ const config = getConfig();
133133
let oauthProvider: OAuthProvider | null = null;
134134

135135
if (config.AUTH_MODE === "full") {
136-
const baseUrl = config.BASE_URL || "http://localhost:3000";
136+
const baseUrl = config.BASE_URL;
137137
oauthProvider = new OAuthProvider({
138138
clientId: "mcp-client",
139139
clientSecret: "mcp-secret",

0 commit comments

Comments
 (0)