Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 19 additions & 24 deletions lib/mcp/gateway.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import {
createGateway,
SignerRefreshRequired,
type InferenceRequest,
type InferenceResult
type InferenceResult,
} from "@pymthouse/gateway-web";
import { pymthouseSignerUrl } from "./env";
import type { McpPrincipal } from "./jwt";
Expand All @@ -12,28 +11,24 @@ export async function runInference(
principal: McpPrincipal,
request: InferenceRequest
): Promise<InferenceResult> {
let session = await resolveSignerSession(principal);
const session = await resolveSignerSession(principal);
const timeoutMs = request.timeoutMs ?? request.timeout ?? 120_000;
let seeded: typeof session | null = session;

const attempt = async (signerJwt: string) => {
const gw = createGateway({
signerUrl: session.signer_url || pymthouseSignerUrl(),
signerHeaders: { Authorization: `Bearer ${signerJwt}` },
discoveryUrl: session.discovery_url,
insecureTls: true,
timeoutMs,
attributionSource: "pymthouse_gateway"
});
return gw.runInference(request);
};

try {
return await attempt(session.access_token);
} catch (err) {
if (err instanceof SignerRefreshRequired) {
session = await resolveSignerSession(principal);
return attempt(session.access_token);
}
throw err;
}
const gw = createGateway({
signerUrl: session.signer_url || pymthouseSignerUrl(),
discoveryUrl: session.discovery_url,
signerHeaders: async () => {
const next = seeded ?? (await resolveSignerSession(principal));
seeded = null;
return {
headers: { Authorization: `Bearer ${next.access_token}` },
expiresInSeconds: next.expires_in,
};
},
insecureTls: true,
timeoutMs,
attributionSource: "pymthouse_gateway",
});
return gw.runInference(request);
}
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"@auth0/nextjs-auth0": "^4.27.0",
"@modelcontextprotocol/sdk": "^1.30.0",
"@pymthouse/builder-sdk": "^0.6.5",
"@pymthouse/gateway-web": "^0.3.0",
"@pymthouse/gateway-web": "^0.4.0",
"framer-motion": "^11.15.0",
"geist": "^1.7.0",
"jose": "^6.2.10",
Expand Down