Skip to content

Commit ca71c1f

Browse files
authored
Fix MCP in production (#3647)
1 parent 262afa3 commit ca71c1f

File tree

1 file changed

+12
-4
lines changed
  • packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/mcp

1 file changed

+12
-4
lines changed

packages/gitbook/src/app/sites/static/[mode]/[siteURL]/[siteData]/~gitbook/mcp/route.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@ import { createMcpHandler } from 'mcp-handler';
66
import type { NextRequest } from 'next/server';
77
import { z } from 'zod';
88

9-
async function handler(request: NextRequest, { params }: { params: Promise<RouteLayoutParams> }) {
9+
async function handler(
10+
nextRequest: NextRequest,
11+
{ params }: { params: Promise<RouteLayoutParams> }
12+
) {
1013
const { context } = await getStaticSiteContext(await params);
1114
const { dataFetcher, linker, site } = context;
1215

@@ -66,9 +69,7 @@ async function handler(request: NextRequest, { params }: { params: Promise<Route
6669
}
6770
);
6871
},
69-
{
70-
// Optional server options
71-
},
72+
{},
7273
{
7374
basePath: context.linker.toPathInSite('~gitbook/'),
7475
streamableHttpEndpoint: '/mcp',
@@ -78,6 +79,13 @@ async function handler(request: NextRequest, { params }: { params: Promise<Route
7879
}
7980
);
8081

82+
// Next.js request.url is the original URL and not the rewritten one from the middleware
83+
const requestURL = new URL(
84+
context.linker.toAbsoluteURL(context.linker.toPathInSite('~gitbook/mcp'))
85+
);
86+
requestURL.search = nextRequest.nextUrl.search;
87+
88+
const request = new Request(requestURL, nextRequest);
8189
return mcpHandler(request);
8290
}
8391

0 commit comments

Comments
 (0)