MCPcopy Create free account
hub / github.com/UsefulSoftwareCo/executor / selfHostHttpMiddleware

Function selfHostHttpMiddleware

apps/host-selfhost/src/serve.ts:51–93  ·  view source on GitHub ↗
(betterAuth: BetterAuthHandle)

Source from the content-addressed store, hash-verified

49// aside from scrubbing any client-supplied value of that header so it can't be
50// spoofed into an unrewritten request.
51const selfHostHttpMiddleware = (betterAuth: BetterAuthHandle) =>
52 HttpMiddleware.make((httpApp) =>
53 Effect.gen(function* () {
54 const request = yield* HttpServerRequest.HttpServerRequest;
55 const url = new URL(request.url, "http://host.internal");
56 if (
57 url.pathname === OAUTH_CALLBACK_PATH &&
58 (request.method === "GET" || request.method === "HEAD")
59 ) {
60 const headers = new Headers(request.headers as Record<string, string>);
61 const webRequest = new Request(url, { method: request.method, headers });
62 const location = yield* Effect.promise(() =>
63 oauthCallbackSignInRedirectLocation(webRequest, betterAuth.auth),
64 );
65 if (location) return HttpServerResponse.redirect(location, { status: 302 });
66 }
67
68 const rewritten = stripMcpOrgSegment(url.pathname);
69 if (rewritten === null) {
70 // Never let a client dictate the org-scoped echo below by smuggling
71 // this header in directly — it's only ever trustworthy when WE set it
72 // a few lines down, for a request we ourselves just rewrote.
73 if (!EffectHeaders.has(request.headers, MCP_ORIGINAL_PATH_HEADER)) return yield* httpApp;
74 return yield* httpApp.pipe(
75 Effect.provideService(
76 HttpServerRequest.HttpServerRequest,
77 request.modify({
78 headers: EffectHeaders.remove(request.headers, MCP_ORIGINAL_PATH_HEADER),
79 }),
80 ),
81 );
82 }
83 return yield* httpApp.pipe(
84 Effect.provideService(
85 HttpServerRequest.HttpServerRequest,
86 request.modify({
87 url: `${rewritten}${url.search}`,
88 headers: EffectHeaders.set(request.headers, MCP_ORIGINAL_PATH_HEADER, url.pathname),
89 }),
90 ),
91 );
92 }),
93 );
94
95export const startServer = async (): Promise<void> => {
96 const config = loadConfig();

Callers 1

startServerFunction · 0.85

Calls 4

stripMcpOrgSegmentFunction · 0.90
setMethod · 0.80
removeMethod · 0.65

Tested by

no test coverage detected