Skip to content

Commit d2356b3

Browse files
committed
Enhance MCP target-url handling and logging
- Added support for case-insensitive retrieval of the x-mcpay-target-url header in the MCP server. - Improved logging to include detailed information about target-url resolution, including both header and query parameter checks. - Updated the MCP proxy route to log all relevant headers for better debugging and visibility.
1 parent 56c4d68 commit d2356b3

File tree

2 files changed

+28
-5
lines changed

2 files changed

+28
-5
lines changed

apps/app/src/app/api/mcp-proxy/route.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,12 @@ export async function POST(request: Request) {
149149
forwardHeaders.delete('mcp-session-id')
150150
}
151151

152+
const targetUrlHeaderValue = forwardHeaders.get('x-mcpay-target-url')
152153
console.log('Forwarding to MCP server with headers:', {
153-
'x-mcpay-target-url': forwardHeaders.get('x-mcpay-target-url')?.substring(0, 50) + '...',
154+
'x-mcpay-target-url': targetUrlHeaderValue ? (targetUrlHeaderValue.substring(0, 50) + '... (full length: ' + targetUrlHeaderValue.length + ')') : 'MISSING',
154155
'content-type': forwardHeaders.get('content-type'),
155156
'authorization': forwardHeaders.get('authorization') ? 'present' : 'missing',
157+
'all-headers': Array.from(forwardHeaders.entries()).map(([k, v]) => [k, k === 'x-mcpay-target-url' ? v.substring(0, 30) + '...' : v.substring(0, 30)]),
156158
})
157159

158160
const response = await fetch(mcpUrl, {

apps/mcp/src/index.ts

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ app.use("*", cors({
4848
"X-Wallet-Type",
4949
"X-Wallet-Address",
5050
"X-Wallet-Provider",
51-
"x-vlayer-enabled"
51+
"x-vlayer-enabled",
52+
"x-mcpay-target-url"
5253
],
5354
allowMethods: ["GET", "POST", "PUT", "DELETE", "OPTIONS"],
5455
credentials: true,
@@ -462,24 +463,44 @@ async function resolveTargetUrl(req: Request, absoluteUrl?: string): Promise<str
462463
return null;
463464
}
464465

465-
const directUrlEncoded = req.headers.get("x-mcpay-target-url")
466-
?? url.searchParams.get("target-url");
466+
// Check header first (case-insensitive)
467+
const headerValue = req.headers.get("x-mcpay-target-url")
468+
?? req.headers.get("X-MCPAY-TARGET-URL")
469+
?? req.headers.get("X-Mcpay-Target-Url");
470+
const queryValue = url.searchParams.get("target-url");
471+
const directUrlEncoded = headerValue ?? queryValue;
472+
473+
const allTargetHeaders: string[] = [];
474+
req.headers.forEach((value, key) => {
475+
if (key.toLowerCase().includes("target")) {
476+
allTargetHeaders.push(`${key}: ${value.substring(0, 30)}...`);
477+
}
478+
});
479+
console.log("[MCP] resolveTargetUrl - checking:", {
480+
hasHeader: !!headerValue,
481+
headerValue: headerValue?.substring(0, 50) + "...",
482+
hasQuery: !!queryValue,
483+
queryValue: queryValue?.substring(0, 50) + "...",
484+
allTargetHeaders
485+
});
467486

468487
if (directUrlEncoded) {
469488
try {
470489
// The value is base64-encoded, so decode it
471490
// decodeURIComponent in case it was URL-encoded as well
472491
const decoded = decodeURIComponent(atob(directUrlEncoded));
492+
console.log("[MCP] Successfully decoded target-url:", decoded.substring(0, 50) + "...");
473493
return decoded;
474494
} catch (e) {
475495
// If decoding fails, try treating it as already decoded (for backwards compatibility)
476496
try {
477497
// Maybe it's already the actual URL?
478498
new URL(directUrlEncoded);
499+
console.log("[MCP] Target-url is already a URL (not base64)");
479500
return directUrlEncoded;
480501
} catch {
481502
// If that also fails, log and fall through
482-
console.log("[MCP] Failed to decode target-url:", e);
503+
console.log("[MCP] Failed to decode target-url:", e, "Value:", directUrlEncoded?.substring(0, 50));
483504
}
484505
}
485506
}

0 commit comments

Comments
 (0)