integrate-sdk 0.7.53 → 0.7.54
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/ai/anthropic.d.ts +16 -22
- package/dist/ai/anthropic.d.ts.map +1 -1
- package/dist/ai/anthropic.js +5 -1
- package/dist/ai/index.js +5 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/server.js +940 -847
- package/dist/src/ai/anthropic.d.ts +16 -22
- package/dist/src/ai/anthropic.d.ts.map +1 -1
- package/dist/src/server.d.ts +9 -0
- package/dist/src/server.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/src/integrations/vercel-ai.d.ts +0 -127
- package/dist/src/integrations/vercel-ai.d.ts.map +0 -1
- package/dist/src/plugins/generic.d.ts +0 -99
- package/dist/src/plugins/generic.d.ts.map +0 -1
- package/dist/src/plugins/github-client.d.ts +0 -320
- package/dist/src/plugins/github-client.d.ts.map +0 -1
- package/dist/src/plugins/github.d.ts +0 -89
- package/dist/src/plugins/github.d.ts.map +0 -1
- package/dist/src/plugins/gmail-client.d.ts +0 -106
- package/dist/src/plugins/gmail-client.d.ts.map +0 -1
- package/dist/src/plugins/gmail.d.ts +0 -87
- package/dist/src/plugins/gmail.d.ts.map +0 -1
- package/dist/src/plugins/server-client.d.ts +0 -18
- package/dist/src/plugins/server-client.d.ts.map +0 -1
- package/dist/src/plugins/types.d.ts +0 -70
- package/dist/src/plugins/types.d.ts.map +0 -1
package/dist/server.js
CHANGED
|
@@ -2365,548 +2365,6 @@ function createSimpleIntegration(config) {
|
|
|
2365
2365
|
onDisconnect: config.onDisconnect
|
|
2366
2366
|
};
|
|
2367
2367
|
}
|
|
2368
|
-
|
|
2369
|
-
// src/server.ts
|
|
2370
|
-
var globalServerConfig = null;
|
|
2371
|
-
function getDefaultRedirectUri() {
|
|
2372
|
-
if (typeof window !== "undefined") {
|
|
2373
|
-
return `${window.location.origin}/api/integrate/oauth/callback`;
|
|
2374
|
-
}
|
|
2375
|
-
const integrateUrl = getEnv("INTEGRATE_URL");
|
|
2376
|
-
if (integrateUrl) {
|
|
2377
|
-
return `${integrateUrl}/api/integrate/oauth/callback`;
|
|
2378
|
-
}
|
|
2379
|
-
const vercelUrl = getEnv("VERCEL_URL");
|
|
2380
|
-
if (vercelUrl) {
|
|
2381
|
-
return `https://${vercelUrl}/api/integrate/oauth/callback`;
|
|
2382
|
-
}
|
|
2383
|
-
return "http://localhost:3000/api/integrate/oauth/callback";
|
|
2384
|
-
}
|
|
2385
|
-
function createMCPServer(config) {
|
|
2386
|
-
if (typeof window !== "undefined") {
|
|
2387
|
-
throw new Error("createMCPServer() should only be called on the server-side. " + "Use createMCPClient() for client-side code.");
|
|
2388
|
-
}
|
|
2389
|
-
const providers = {};
|
|
2390
|
-
const updatedIntegrations = config.integrations.map((integration) => {
|
|
2391
|
-
if (integration.oauth) {
|
|
2392
|
-
const { clientId, clientSecret, redirectUri: integrationRedirectUri, config: oauthConfig } = integration.oauth;
|
|
2393
|
-
if (!clientId || !clientSecret) {
|
|
2394
|
-
console.warn(`Warning: Integration "${integration.id}" is missing OAuth credentials. ` + `Provide clientId and clientSecret in the integration configuration.`);
|
|
2395
|
-
return integration;
|
|
2396
|
-
}
|
|
2397
|
-
const redirectUri = integrationRedirectUri || config.redirectUri || getDefaultRedirectUri();
|
|
2398
|
-
providers[integration.id] = {
|
|
2399
|
-
clientId,
|
|
2400
|
-
clientSecret,
|
|
2401
|
-
redirectUri,
|
|
2402
|
-
config: oauthConfig
|
|
2403
|
-
};
|
|
2404
|
-
return {
|
|
2405
|
-
...integration,
|
|
2406
|
-
oauth: {
|
|
2407
|
-
...integration.oauth,
|
|
2408
|
-
redirectUri
|
|
2409
|
-
}
|
|
2410
|
-
};
|
|
2411
|
-
}
|
|
2412
|
-
return integration;
|
|
2413
|
-
});
|
|
2414
|
-
globalServerConfig = {
|
|
2415
|
-
providers,
|
|
2416
|
-
serverUrl: config.serverUrl,
|
|
2417
|
-
apiKey: config.apiKey
|
|
2418
|
-
};
|
|
2419
|
-
const clientConfig = {
|
|
2420
|
-
...config,
|
|
2421
|
-
integrations: updatedIntegrations,
|
|
2422
|
-
connectionMode: config.connectionMode || "lazy",
|
|
2423
|
-
singleton: config.singleton ?? true
|
|
2424
|
-
};
|
|
2425
|
-
const client = new MCPClientBase(clientConfig);
|
|
2426
|
-
if (config.apiKey) {
|
|
2427
|
-
client.setRequestHeader("X-API-KEY", config.apiKey);
|
|
2428
|
-
}
|
|
2429
|
-
client.__oauthConfig = {
|
|
2430
|
-
providers,
|
|
2431
|
-
serverUrl: config.serverUrl,
|
|
2432
|
-
apiKey: config.apiKey,
|
|
2433
|
-
getSessionContext: config.getSessionContext,
|
|
2434
|
-
setProviderToken: config.setProviderToken
|
|
2435
|
-
};
|
|
2436
|
-
const { POST, GET } = createOAuthRouteHandlers({
|
|
2437
|
-
providers,
|
|
2438
|
-
serverUrl: config.serverUrl,
|
|
2439
|
-
apiKey: config.apiKey,
|
|
2440
|
-
getSessionContext: config.getSessionContext,
|
|
2441
|
-
setProviderToken: config.setProviderToken
|
|
2442
|
-
});
|
|
2443
|
-
const handler = async (request, context) => {
|
|
2444
|
-
const method = request.method.toUpperCase();
|
|
2445
|
-
let action;
|
|
2446
|
-
let segments = [];
|
|
2447
|
-
if (context?.params?.action) {
|
|
2448
|
-
action = context.params.action;
|
|
2449
|
-
} else if (context?.params?.all) {
|
|
2450
|
-
const all = context.params.all;
|
|
2451
|
-
if (Array.isArray(all)) {
|
|
2452
|
-
segments = all;
|
|
2453
|
-
} else if (typeof all === "string") {
|
|
2454
|
-
segments = all.split("/").filter(Boolean);
|
|
2455
|
-
}
|
|
2456
|
-
if (segments.length === 2 && segments[0] === "oauth") {
|
|
2457
|
-
action = segments[1];
|
|
2458
|
-
} else if (segments.length === 1) {
|
|
2459
|
-
if (segments[0] === "mcp") {
|
|
2460
|
-
action = "mcp";
|
|
2461
|
-
} else {
|
|
2462
|
-
action = segments[0];
|
|
2463
|
-
}
|
|
2464
|
-
} else if (segments.length > 0) {
|
|
2465
|
-
action = segments[segments.length - 1];
|
|
2466
|
-
}
|
|
2467
|
-
} else {
|
|
2468
|
-
const url = new URL(request.url);
|
|
2469
|
-
const pathParts = url.pathname.split("/").filter(Boolean);
|
|
2470
|
-
segments = pathParts;
|
|
2471
|
-
const oauthIndex = pathParts.indexOf("oauth");
|
|
2472
|
-
if (oauthIndex >= 0 && oauthIndex < pathParts.length - 1) {
|
|
2473
|
-
action = pathParts[oauthIndex + 1];
|
|
2474
|
-
} else if (pathParts.length > 0) {
|
|
2475
|
-
action = pathParts[pathParts.length - 1];
|
|
2476
|
-
} else {
|
|
2477
|
-
action = "callback";
|
|
2478
|
-
}
|
|
2479
|
-
}
|
|
2480
|
-
if (action === "mcp" && method === "POST") {
|
|
2481
|
-
try {
|
|
2482
|
-
const body = await request.json();
|
|
2483
|
-
const authHeader = request.headers.get("authorization");
|
|
2484
|
-
const { OAuthHandler: OAuthHandler2 } = await Promise.resolve().then(() => exports_base_handler);
|
|
2485
|
-
const oauthHandler = new OAuthHandler2({
|
|
2486
|
-
providers,
|
|
2487
|
-
serverUrl: config.serverUrl,
|
|
2488
|
-
apiKey: config.apiKey
|
|
2489
|
-
});
|
|
2490
|
-
const result = await oauthHandler.handleToolCall(body, authHeader);
|
|
2491
|
-
return Response.json(result);
|
|
2492
|
-
} catch (error) {
|
|
2493
|
-
console.error("[MCP Tool Call] Error:", error);
|
|
2494
|
-
return Response.json({ error: error.message || "Failed to execute tool call" }, { status: error.statusCode || 500 });
|
|
2495
|
-
}
|
|
2496
|
-
}
|
|
2497
|
-
if (segments.length > 0) {
|
|
2498
|
-
if (segments.length === 2 && segments[0] !== "oauth") {
|
|
2499
|
-
return Response.json({ error: `Invalid route: /${segments.join("/")}` }, { status: 404 });
|
|
2500
|
-
}
|
|
2501
|
-
if (segments.length === 1 && segments[0] === "mcp") {
|
|
2502
|
-
return Response.json({ error: `Method ${method} not allowed for /mcp route. Use POST.` }, { status: 405 });
|
|
2503
|
-
}
|
|
2504
|
-
}
|
|
2505
|
-
if (method === "GET" && action === "callback") {
|
|
2506
|
-
const url = new URL(request.url);
|
|
2507
|
-
const searchParams = url.searchParams;
|
|
2508
|
-
const code = searchParams.get("code");
|
|
2509
|
-
const state = searchParams.get("state");
|
|
2510
|
-
const error = searchParams.get("error");
|
|
2511
|
-
const errorDescription = searchParams.get("error_description");
|
|
2512
|
-
const defaultRedirectUrl = "/";
|
|
2513
|
-
const errorRedirectUrl = "/auth-error";
|
|
2514
|
-
if (error) {
|
|
2515
|
-
const errorMsg = errorDescription || error;
|
|
2516
|
-
console.error("[OAuth Redirect] Error:", errorMsg);
|
|
2517
|
-
return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent(errorMsg)}`, request.url));
|
|
2518
|
-
}
|
|
2519
|
-
if (!code || !state) {
|
|
2520
|
-
console.error("[OAuth Redirect] Missing code or state parameter");
|
|
2521
|
-
return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent("Invalid OAuth callback")}`, request.url));
|
|
2522
|
-
}
|
|
2523
|
-
let returnUrl = defaultRedirectUrl;
|
|
2524
|
-
try {
|
|
2525
|
-
const { parseState: parseState2 } = await Promise.resolve().then(() => exports_pkce);
|
|
2526
|
-
const stateData = parseState2(state);
|
|
2527
|
-
if (stateData.returnUrl) {
|
|
2528
|
-
returnUrl = stateData.returnUrl;
|
|
2529
|
-
}
|
|
2530
|
-
} catch (e) {
|
|
2531
|
-
try {
|
|
2532
|
-
const referrer = request.headers.get("referer") || request.headers.get("referrer");
|
|
2533
|
-
if (referrer) {
|
|
2534
|
-
const referrerUrl = new URL(referrer);
|
|
2535
|
-
const currentUrl = new URL(request.url);
|
|
2536
|
-
if (referrerUrl.origin === currentUrl.origin) {
|
|
2537
|
-
returnUrl = referrerUrl.pathname + referrerUrl.search;
|
|
2538
|
-
}
|
|
2539
|
-
}
|
|
2540
|
-
} catch {}
|
|
2541
|
-
}
|
|
2542
|
-
const targetUrl = new URL(returnUrl, request.url);
|
|
2543
|
-
targetUrl.hash = `oauth_callback=${encodeURIComponent(JSON.stringify({ code, state }))}`;
|
|
2544
|
-
return Response.redirect(targetUrl);
|
|
2545
|
-
}
|
|
2546
|
-
const handlerContext = { params: { action: action || "callback" } };
|
|
2547
|
-
if (method === "POST") {
|
|
2548
|
-
return POST(request, handlerContext);
|
|
2549
|
-
} else if (method === "GET") {
|
|
2550
|
-
return GET(request, handlerContext);
|
|
2551
|
-
} else {
|
|
2552
|
-
return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
|
|
2553
|
-
}
|
|
2554
|
-
};
|
|
2555
|
-
const serverClient = client;
|
|
2556
|
-
serverClient.handler = handler;
|
|
2557
|
-
serverClient.POST = POST;
|
|
2558
|
-
serverClient.GET = GET;
|
|
2559
|
-
return {
|
|
2560
|
-
client: serverClient,
|
|
2561
|
-
POST,
|
|
2562
|
-
GET
|
|
2563
|
-
};
|
|
2564
|
-
}
|
|
2565
|
-
function createOAuthRouteHandlers(config) {
|
|
2566
|
-
const handler = createNextOAuthHandler(config);
|
|
2567
|
-
return handler.createRoutes();
|
|
2568
|
-
}
|
|
2569
|
-
var POST = async (req, context) => {
|
|
2570
|
-
if (!globalServerConfig) {
|
|
2571
|
-
return Response.json({ error: "OAuth not configured. Call createMCPServer() in your server initialization file first." }, { status: 500 });
|
|
2572
|
-
}
|
|
2573
|
-
const handler = createNextOAuthHandler(globalServerConfig);
|
|
2574
|
-
const routes = handler.createRoutes();
|
|
2575
|
-
return routes.POST(req, context);
|
|
2576
|
-
};
|
|
2577
|
-
var GET = async (req, context) => {
|
|
2578
|
-
if (!globalServerConfig) {
|
|
2579
|
-
return Response.json({ error: "OAuth not configured. Call createMCPServer() in your server initialization file first." }, { status: 500 });
|
|
2580
|
-
}
|
|
2581
|
-
const handler = createNextOAuthHandler(globalServerConfig);
|
|
2582
|
-
const routes = handler.createRoutes();
|
|
2583
|
-
return routes.GET(req, context);
|
|
2584
|
-
};
|
|
2585
|
-
function toNextJsHandler(clientOrOptions, redirectOptions) {
|
|
2586
|
-
let config;
|
|
2587
|
-
let redirectUrl;
|
|
2588
|
-
let errorRedirectUrl;
|
|
2589
|
-
if (!clientOrOptions) {
|
|
2590
|
-
config = globalServerConfig;
|
|
2591
|
-
redirectUrl = redirectOptions?.redirectUrl;
|
|
2592
|
-
errorRedirectUrl = redirectOptions?.errorRedirectUrl;
|
|
2593
|
-
} else if (clientOrOptions.__oauthConfig) {
|
|
2594
|
-
config = clientOrOptions.__oauthConfig;
|
|
2595
|
-
redirectUrl = redirectOptions?.redirectUrl;
|
|
2596
|
-
errorRedirectUrl = redirectOptions?.errorRedirectUrl;
|
|
2597
|
-
} else if (typeof clientOrOptions === "object" && clientOrOptions.providers) {
|
|
2598
|
-
const options = clientOrOptions;
|
|
2599
|
-
config = {
|
|
2600
|
-
providers: options.providers,
|
|
2601
|
-
serverUrl: options.serverUrl,
|
|
2602
|
-
apiKey: options.apiKey,
|
|
2603
|
-
getSessionContext: options.getSessionContext,
|
|
2604
|
-
setProviderToken: options.setProviderToken
|
|
2605
|
-
};
|
|
2606
|
-
redirectUrl = options.redirectUrl;
|
|
2607
|
-
errorRedirectUrl = options.errorRedirectUrl;
|
|
2608
|
-
} else {
|
|
2609
|
-
config = null;
|
|
2610
|
-
}
|
|
2611
|
-
if (!config) {
|
|
2612
|
-
throw new Error(`toNextJsHandler requires either:
|
|
2613
|
-
1. A client instance from createMCPServer()
|
|
2614
|
-
2. A config object with providers property
|
|
2615
|
-
3. No arguments (uses global config from createMCPServer)`);
|
|
2616
|
-
}
|
|
2617
|
-
const POST2 = async (req, context) => {
|
|
2618
|
-
const handler = createNextOAuthHandler(config);
|
|
2619
|
-
const routes = handler.toNextJsHandler({
|
|
2620
|
-
redirectUrl,
|
|
2621
|
-
errorRedirectUrl
|
|
2622
|
-
});
|
|
2623
|
-
return routes.POST(req, context);
|
|
2624
|
-
};
|
|
2625
|
-
const GET2 = async (req, context) => {
|
|
2626
|
-
const handler = createNextOAuthHandler(config);
|
|
2627
|
-
const routes = handler.toNextJsHandler({
|
|
2628
|
-
redirectUrl,
|
|
2629
|
-
errorRedirectUrl
|
|
2630
|
-
});
|
|
2631
|
-
return routes.GET(req, context);
|
|
2632
|
-
};
|
|
2633
|
-
return { POST: POST2, GET: GET2 };
|
|
2634
|
-
}
|
|
2635
|
-
function toSolidStartHandler(clientOrHandlerOrOptions, _redirectOptions) {
|
|
2636
|
-
if (clientOrHandlerOrOptions && clientOrHandlerOrOptions.handler && typeof clientOrHandlerOrOptions.handler === "function") {
|
|
2637
|
-
const baseHandler = clientOrHandlerOrOptions.handler;
|
|
2638
|
-
const handler2 = async (event) => {
|
|
2639
|
-
return baseHandler(event.request);
|
|
2640
|
-
};
|
|
2641
|
-
return {
|
|
2642
|
-
GET: handler2,
|
|
2643
|
-
POST: handler2,
|
|
2644
|
-
PATCH: handler2,
|
|
2645
|
-
PUT: handler2,
|
|
2646
|
-
DELETE: handler2
|
|
2647
|
-
};
|
|
2648
|
-
}
|
|
2649
|
-
if (typeof clientOrHandlerOrOptions === "function") {
|
|
2650
|
-
const baseHandler = clientOrHandlerOrOptions;
|
|
2651
|
-
const handler2 = async (event) => {
|
|
2652
|
-
return baseHandler(event.request);
|
|
2653
|
-
};
|
|
2654
|
-
return {
|
|
2655
|
-
GET: handler2,
|
|
2656
|
-
POST: handler2,
|
|
2657
|
-
PATCH: handler2,
|
|
2658
|
-
PUT: handler2,
|
|
2659
|
-
DELETE: handler2
|
|
2660
|
-
};
|
|
2661
|
-
}
|
|
2662
|
-
const options = clientOrHandlerOrOptions;
|
|
2663
|
-
if (!options.providers || Object.keys(options.providers).length === 0) {
|
|
2664
|
-
throw new Error("toSolidStartHandler requires either a handler function or a providers config object");
|
|
2665
|
-
}
|
|
2666
|
-
const { providers, serverUrl, apiKey, redirectUrl, errorRedirectUrl } = options;
|
|
2667
|
-
const nextHandler = createNextOAuthHandler({
|
|
2668
|
-
providers,
|
|
2669
|
-
serverUrl,
|
|
2670
|
-
apiKey
|
|
2671
|
-
});
|
|
2672
|
-
const routes = nextHandler.toNextJsHandler({
|
|
2673
|
-
redirectUrl: redirectUrl || "/",
|
|
2674
|
-
errorRedirectUrl: errorRedirectUrl || "/auth-error"
|
|
2675
|
-
});
|
|
2676
|
-
const handler = async (event) => {
|
|
2677
|
-
const method = event.request.method.toUpperCase();
|
|
2678
|
-
const url = new URL(event.request.url);
|
|
2679
|
-
const pathParts = url.pathname.split("/").filter(Boolean);
|
|
2680
|
-
const integrateIndex = pathParts.indexOf("integrate");
|
|
2681
|
-
const segments = integrateIndex >= 0 ? pathParts.slice(integrateIndex + 1) : [];
|
|
2682
|
-
const context = {
|
|
2683
|
-
params: segments
|
|
2684
|
-
};
|
|
2685
|
-
if (method === "POST") {
|
|
2686
|
-
return routes.POST(event.request, { params: { all: context.params } });
|
|
2687
|
-
} else if (method === "GET") {
|
|
2688
|
-
return routes.GET(event.request, { params: { all: context.params } });
|
|
2689
|
-
} else {
|
|
2690
|
-
return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
|
|
2691
|
-
}
|
|
2692
|
-
};
|
|
2693
|
-
return {
|
|
2694
|
-
GET: handler,
|
|
2695
|
-
POST: handler,
|
|
2696
|
-
PATCH: handler,
|
|
2697
|
-
PUT: handler,
|
|
2698
|
-
DELETE: handler
|
|
2699
|
-
};
|
|
2700
|
-
}
|
|
2701
|
-
function toSvelteKitHandler(clientOrHandlerOrOptions, _redirectOptions) {
|
|
2702
|
-
if (clientOrHandlerOrOptions && clientOrHandlerOrOptions.handler && typeof clientOrHandlerOrOptions.handler === "function") {
|
|
2703
|
-
const baseHandler = clientOrHandlerOrOptions.handler;
|
|
2704
|
-
return async (event) => {
|
|
2705
|
-
const all = event.params?.all;
|
|
2706
|
-
return baseHandler(event.request, { params: { all } });
|
|
2707
|
-
};
|
|
2708
|
-
}
|
|
2709
|
-
if (typeof clientOrHandlerOrOptions === "function") {
|
|
2710
|
-
const baseHandler = clientOrHandlerOrOptions;
|
|
2711
|
-
return async (event) => {
|
|
2712
|
-
const all = event.params?.all;
|
|
2713
|
-
return baseHandler(event.request, { params: { all } });
|
|
2714
|
-
};
|
|
2715
|
-
}
|
|
2716
|
-
const options = clientOrHandlerOrOptions;
|
|
2717
|
-
if (!options.providers || Object.keys(options.providers).length === 0) {
|
|
2718
|
-
throw new Error("toSvelteKitHandler requires either a handler function or a providers config object");
|
|
2719
|
-
}
|
|
2720
|
-
const { providers, serverUrl, apiKey, redirectUrl, errorRedirectUrl } = options;
|
|
2721
|
-
const nextHandler = createNextOAuthHandler({
|
|
2722
|
-
providers,
|
|
2723
|
-
serverUrl,
|
|
2724
|
-
apiKey
|
|
2725
|
-
});
|
|
2726
|
-
const routes = nextHandler.toNextJsHandler({
|
|
2727
|
-
redirectUrl: redirectUrl || "/",
|
|
2728
|
-
errorRedirectUrl: errorRedirectUrl || "/auth-error"
|
|
2729
|
-
});
|
|
2730
|
-
return async (event) => {
|
|
2731
|
-
const method = event.request.method.toUpperCase();
|
|
2732
|
-
const all = event.params?.all;
|
|
2733
|
-
const context = {
|
|
2734
|
-
params: Array.isArray(all) ? all : all ? all.split("/").filter(Boolean) : []
|
|
2735
|
-
};
|
|
2736
|
-
if (method === "POST") {
|
|
2737
|
-
return routes.POST(event.request, { params: { all: context.params } });
|
|
2738
|
-
} else if (method === "GET") {
|
|
2739
|
-
return routes.GET(event.request, { params: { all: context.params } });
|
|
2740
|
-
} else {
|
|
2741
|
-
return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
|
|
2742
|
-
}
|
|
2743
|
-
};
|
|
2744
|
-
}
|
|
2745
|
-
// src/adapters/node.ts
|
|
2746
|
-
function fromNodeHeaders(nodeHeaders) {
|
|
2747
|
-
const webHeaders = new Headers;
|
|
2748
|
-
for (const [key, value] of Object.entries(nodeHeaders)) {
|
|
2749
|
-
if (value !== undefined) {
|
|
2750
|
-
if (Array.isArray(value)) {
|
|
2751
|
-
value.forEach((v) => webHeaders.append(key, v));
|
|
2752
|
-
} else {
|
|
2753
|
-
webHeaders.set(key, value);
|
|
2754
|
-
}
|
|
2755
|
-
}
|
|
2756
|
-
}
|
|
2757
|
-
return webHeaders;
|
|
2758
|
-
}
|
|
2759
|
-
async function toWebRequest(req) {
|
|
2760
|
-
const protocol = req.socket.encrypted ? "https" : "http";
|
|
2761
|
-
const host = req.headers.host || "localhost";
|
|
2762
|
-
const url = `${protocol}://${host}${req.url}`;
|
|
2763
|
-
const headers = fromNodeHeaders(req.headers);
|
|
2764
|
-
let body;
|
|
2765
|
-
if (req.method && ["POST", "PUT", "PATCH"].includes(req.method)) {
|
|
2766
|
-
body = await new Promise((resolve, reject) => {
|
|
2767
|
-
let data = "";
|
|
2768
|
-
req.on("data", (chunk) => data += chunk);
|
|
2769
|
-
req.on("end", () => resolve(data));
|
|
2770
|
-
req.on("error", reject);
|
|
2771
|
-
});
|
|
2772
|
-
}
|
|
2773
|
-
return new Request(url, {
|
|
2774
|
-
method: req.method,
|
|
2775
|
-
headers,
|
|
2776
|
-
body: body || undefined
|
|
2777
|
-
});
|
|
2778
|
-
}
|
|
2779
|
-
async function sendWebResponse(webRes, nodeRes) {
|
|
2780
|
-
nodeRes.statusCode = webRes.status;
|
|
2781
|
-
webRes.headers.forEach((value, key) => {
|
|
2782
|
-
nodeRes.setHeader(key, value);
|
|
2783
|
-
});
|
|
2784
|
-
const body = await webRes.text();
|
|
2785
|
-
nodeRes.end(body);
|
|
2786
|
-
}
|
|
2787
|
-
function toNodeHandler(config) {
|
|
2788
|
-
const oauthHandler = new OAuthHandler(config);
|
|
2789
|
-
return async (req, res) => {
|
|
2790
|
-
try {
|
|
2791
|
-
const webReq = await toWebRequest(req);
|
|
2792
|
-
const url = new URL(webReq.url);
|
|
2793
|
-
const segments = url.pathname.split("/").filter(Boolean);
|
|
2794
|
-
const action = segments[segments.length - 1];
|
|
2795
|
-
let webRes;
|
|
2796
|
-
if (req.method === "POST") {
|
|
2797
|
-
if (action === "authorize") {
|
|
2798
|
-
const result = await oauthHandler.handleAuthorize(webReq);
|
|
2799
|
-
const headers = { "Content-Type": "application/json" };
|
|
2800
|
-
if (result.setCookie) {
|
|
2801
|
-
headers["Set-Cookie"] = result.setCookie;
|
|
2802
|
-
}
|
|
2803
|
-
webRes = new Response(JSON.stringify(result), {
|
|
2804
|
-
status: 200,
|
|
2805
|
-
headers
|
|
2806
|
-
});
|
|
2807
|
-
} else if (action === "callback") {
|
|
2808
|
-
const result = await oauthHandler.handleCallback(webReq);
|
|
2809
|
-
const headers = { "Content-Type": "application/json" };
|
|
2810
|
-
if (result.clearCookie) {
|
|
2811
|
-
headers["Set-Cookie"] = result.clearCookie;
|
|
2812
|
-
}
|
|
2813
|
-
webRes = new Response(JSON.stringify(result), {
|
|
2814
|
-
status: 200,
|
|
2815
|
-
headers
|
|
2816
|
-
});
|
|
2817
|
-
} else if (action === "disconnect") {
|
|
2818
|
-
const authHeader = webReq.headers.get("authorization");
|
|
2819
|
-
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
2820
|
-
webRes = new Response(JSON.stringify({ error: "Missing or invalid Authorization header" }), {
|
|
2821
|
-
status: 400,
|
|
2822
|
-
headers: { "Content-Type": "application/json" }
|
|
2823
|
-
});
|
|
2824
|
-
} else {
|
|
2825
|
-
const accessToken = authHeader.substring(7);
|
|
2826
|
-
const body = await webReq.json();
|
|
2827
|
-
const { provider } = body;
|
|
2828
|
-
if (!provider) {
|
|
2829
|
-
webRes = new Response(JSON.stringify({ error: "Missing provider in request body" }), {
|
|
2830
|
-
status: 400,
|
|
2831
|
-
headers: { "Content-Type": "application/json" }
|
|
2832
|
-
});
|
|
2833
|
-
} else {
|
|
2834
|
-
const result = await oauthHandler.handleDisconnect({ provider }, accessToken);
|
|
2835
|
-
webRes = new Response(JSON.stringify(result), {
|
|
2836
|
-
status: 200,
|
|
2837
|
-
headers: { "Content-Type": "application/json" }
|
|
2838
|
-
});
|
|
2839
|
-
}
|
|
2840
|
-
}
|
|
2841
|
-
} else {
|
|
2842
|
-
webRes = new Response(JSON.stringify({ error: `Unknown action: ${action}` }), {
|
|
2843
|
-
status: 404,
|
|
2844
|
-
headers: { "Content-Type": "application/json" }
|
|
2845
|
-
});
|
|
2846
|
-
}
|
|
2847
|
-
} else if (req.method === "GET" && action === "status") {
|
|
2848
|
-
const provider = url.searchParams.get("provider");
|
|
2849
|
-
const authHeader = webReq.headers.get("authorization");
|
|
2850
|
-
if (!provider) {
|
|
2851
|
-
webRes = new Response(JSON.stringify({ error: "Missing provider query parameter" }), {
|
|
2852
|
-
status: 400,
|
|
2853
|
-
headers: { "Content-Type": "application/json" }
|
|
2854
|
-
});
|
|
2855
|
-
} else if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
2856
|
-
webRes = new Response(JSON.stringify({ error: "Missing or invalid Authorization header" }), {
|
|
2857
|
-
status: 400,
|
|
2858
|
-
headers: { "Content-Type": "application/json" }
|
|
2859
|
-
});
|
|
2860
|
-
} else {
|
|
2861
|
-
const accessToken = authHeader.substring(7);
|
|
2862
|
-
const result = await oauthHandler.handleStatus(provider, accessToken);
|
|
2863
|
-
webRes = new Response(JSON.stringify(result), {
|
|
2864
|
-
status: 200,
|
|
2865
|
-
headers: { "Content-Type": "application/json" }
|
|
2866
|
-
});
|
|
2867
|
-
}
|
|
2868
|
-
} else {
|
|
2869
|
-
webRes = new Response(JSON.stringify({ error: `Unknown action: ${action}` }), {
|
|
2870
|
-
status: 404,
|
|
2871
|
-
headers: { "Content-Type": "application/json" }
|
|
2872
|
-
});
|
|
2873
|
-
}
|
|
2874
|
-
await sendWebResponse(webRes, res);
|
|
2875
|
-
} catch (error) {
|
|
2876
|
-
console.error("[OAuth Handler] Error:", error);
|
|
2877
|
-
const errorRes = new Response(JSON.stringify({ error: error.message || "Internal server error" }), {
|
|
2878
|
-
status: 500,
|
|
2879
|
-
headers: { "Content-Type": "application/json" }
|
|
2880
|
-
});
|
|
2881
|
-
await sendWebResponse(errorRes, res);
|
|
2882
|
-
}
|
|
2883
|
-
};
|
|
2884
|
-
}
|
|
2885
|
-
// src/adapters/svelte-kit.ts
|
|
2886
|
-
async function svelteKitHandler({
|
|
2887
|
-
authConfig,
|
|
2888
|
-
event,
|
|
2889
|
-
resolve,
|
|
2890
|
-
basePath = "/api/integrate"
|
|
2891
|
-
}) {
|
|
2892
|
-
const { url } = event;
|
|
2893
|
-
const baseUrl = new URL(basePath, url.origin);
|
|
2894
|
-
if (!url.pathname.startsWith(baseUrl.pathname)) {
|
|
2895
|
-
return resolve(event);
|
|
2896
|
-
}
|
|
2897
|
-
return authConfig(event.request);
|
|
2898
|
-
}
|
|
2899
|
-
// src/adapters/tanstack-start.ts
|
|
2900
|
-
function toTanStackStartHandler(handler) {
|
|
2901
|
-
const baseHandler = async ({ request }) => {
|
|
2902
|
-
return handler(request);
|
|
2903
|
-
};
|
|
2904
|
-
return {
|
|
2905
|
-
GET: baseHandler,
|
|
2906
|
-
POST: baseHandler
|
|
2907
|
-
};
|
|
2908
|
-
}
|
|
2909
|
-
var createTanStackOAuthHandler = toTanStackStartHandler;
|
|
2910
2368
|
// node_modules/zod/v3/external.js
|
|
2911
2369
|
var exports_external = {};
|
|
2912
2370
|
__export(exports_external, {
|
|
@@ -7026,78 +6484,342 @@ function jsonSchemaToZod(schema) {
|
|
|
7026
6484
|
}
|
|
7027
6485
|
return exports_external.object({});
|
|
7028
6486
|
}
|
|
7029
|
-
if (schema.type !== "object") {
|
|
7030
|
-
return exports_external.object({});
|
|
6487
|
+
if (schema.type !== "object") {
|
|
6488
|
+
return exports_external.object({});
|
|
6489
|
+
}
|
|
6490
|
+
if (schema.properties && typeof schema.properties === "object") {
|
|
6491
|
+
const shape = {};
|
|
6492
|
+
const required = schema.required || [];
|
|
6493
|
+
for (const [key, value] of Object.entries(schema.properties)) {
|
|
6494
|
+
let propSchema = jsonSchemaPropertyToZod(value);
|
|
6495
|
+
if (!required.includes(key)) {
|
|
6496
|
+
propSchema = propSchema.optional();
|
|
6497
|
+
}
|
|
6498
|
+
shape[key] = propSchema;
|
|
6499
|
+
}
|
|
6500
|
+
return exports_external.object(shape);
|
|
6501
|
+
}
|
|
6502
|
+
return exports_external.object({});
|
|
6503
|
+
}
|
|
6504
|
+
async function executeToolWithToken(client, toolName, args, options) {
|
|
6505
|
+
if (options?.providerTokens) {
|
|
6506
|
+
const provider = getProviderForTool(client, toolName);
|
|
6507
|
+
if (provider && options.providerTokens[provider]) {
|
|
6508
|
+
const oauthManager = client.oauthManager;
|
|
6509
|
+
if (oauthManager && typeof oauthManager.setProviderToken === "function") {
|
|
6510
|
+
const existingToken = oauthManager.getProviderToken(provider);
|
|
6511
|
+
try {
|
|
6512
|
+
oauthManager.setProviderToken(provider, {
|
|
6513
|
+
accessToken: options.providerTokens[provider],
|
|
6514
|
+
tokenType: "Bearer"
|
|
6515
|
+
});
|
|
6516
|
+
const result2 = await client._callToolByName(toolName, args);
|
|
6517
|
+
return result2;
|
|
6518
|
+
} finally {
|
|
6519
|
+
if (existingToken) {
|
|
6520
|
+
oauthManager.setProviderToken(provider, existingToken);
|
|
6521
|
+
} else if (typeof oauthManager.removeProviderToken === "function") {
|
|
6522
|
+
oauthManager.removeProviderToken(provider);
|
|
6523
|
+
}
|
|
6524
|
+
}
|
|
6525
|
+
}
|
|
6526
|
+
}
|
|
6527
|
+
}
|
|
6528
|
+
const result = await client._callToolByName(toolName, args);
|
|
6529
|
+
return result;
|
|
6530
|
+
}
|
|
6531
|
+
async function ensureClientConnected(client) {
|
|
6532
|
+
if (!client.isConnected()) {
|
|
6533
|
+
await client.connect();
|
|
6534
|
+
}
|
|
6535
|
+
}
|
|
6536
|
+
|
|
6537
|
+
// src/ai/vercel-ai.ts
|
|
6538
|
+
function convertMCPToolToVercelAI(mcpTool, client, options) {
|
|
6539
|
+
return {
|
|
6540
|
+
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
6541
|
+
inputSchema: jsonSchemaToZod(mcpTool.inputSchema),
|
|
6542
|
+
execute: async (args) => {
|
|
6543
|
+
if (options?.providerTokens) {
|
|
6544
|
+
return await executeToolWithToken(client, mcpTool.name, args, options);
|
|
6545
|
+
}
|
|
6546
|
+
return await client._callToolByName(mcpTool.name, args, options?.context ? { context: options.context } : undefined);
|
|
6547
|
+
}
|
|
6548
|
+
};
|
|
6549
|
+
}
|
|
6550
|
+
function convertMCPToolsToVercelAI(client, options) {
|
|
6551
|
+
const mcpTools = client.getEnabledTools();
|
|
6552
|
+
const vercelTools = {};
|
|
6553
|
+
for (const mcpTool of mcpTools) {
|
|
6554
|
+
vercelTools[mcpTool.name] = convertMCPToolToVercelAI(mcpTool, client, options);
|
|
6555
|
+
}
|
|
6556
|
+
return vercelTools;
|
|
6557
|
+
}
|
|
6558
|
+
async function getVercelAITools(client, options) {
|
|
6559
|
+
await ensureClientConnected(client);
|
|
6560
|
+
let providerTokens = options?.providerTokens;
|
|
6561
|
+
if (!providerTokens) {
|
|
6562
|
+
try {
|
|
6563
|
+
providerTokens = await getProviderTokens();
|
|
6564
|
+
} catch {}
|
|
6565
|
+
}
|
|
6566
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6567
|
+
return convertMCPToolsToVercelAI(client, finalOptions);
|
|
6568
|
+
}
|
|
6569
|
+
// src/ai/openai.ts
|
|
6570
|
+
function convertMCPToolToOpenAI(mcpTool, _client, options) {
|
|
6571
|
+
const inputParams = mcpTool.inputSchema;
|
|
6572
|
+
return {
|
|
6573
|
+
type: "function",
|
|
6574
|
+
name: mcpTool.name,
|
|
6575
|
+
parameters: inputParams || null,
|
|
6576
|
+
strict: options?.strict ?? null,
|
|
6577
|
+
description: mcpTool.description || null
|
|
6578
|
+
};
|
|
6579
|
+
}
|
|
6580
|
+
function convertMCPToolsToOpenAI(client, options) {
|
|
6581
|
+
const mcpTools = client.getEnabledTools();
|
|
6582
|
+
return mcpTools.map((mcpTool) => convertMCPToolToOpenAI(mcpTool, client, options));
|
|
6583
|
+
}
|
|
6584
|
+
async function executeOpenAIToolCall(client, toolCall, options) {
|
|
6585
|
+
const args = JSON.parse(toolCall.arguments);
|
|
6586
|
+
const result = await executeToolWithToken(client, toolCall.name, args, options);
|
|
6587
|
+
return JSON.stringify(result);
|
|
6588
|
+
}
|
|
6589
|
+
async function getOpenAITools(client, options) {
|
|
6590
|
+
await ensureClientConnected(client);
|
|
6591
|
+
let providerTokens = options?.providerTokens;
|
|
6592
|
+
if (!providerTokens) {
|
|
6593
|
+
try {
|
|
6594
|
+
providerTokens = await getProviderTokens();
|
|
6595
|
+
} catch {}
|
|
6596
|
+
}
|
|
6597
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6598
|
+
return convertMCPToolsToOpenAI(client, finalOptions);
|
|
6599
|
+
}
|
|
6600
|
+
async function handleOpenAIToolCalls(client, toolCalls, options) {
|
|
6601
|
+
const toolOutputs = [];
|
|
6602
|
+
for (const output of toolCalls) {
|
|
6603
|
+
if (output.type === "function_call") {
|
|
6604
|
+
const toolCall = {
|
|
6605
|
+
id: output.id ?? "",
|
|
6606
|
+
name: output.name,
|
|
6607
|
+
arguments: output.arguments
|
|
6608
|
+
};
|
|
6609
|
+
try {
|
|
6610
|
+
const result = await executeOpenAIToolCall(client, toolCall, options);
|
|
6611
|
+
toolOutputs.push({
|
|
6612
|
+
call_id: output.call_id ?? output.id ?? "",
|
|
6613
|
+
type: "function_call_output",
|
|
6614
|
+
output: result,
|
|
6615
|
+
status: "completed"
|
|
6616
|
+
});
|
|
6617
|
+
} catch (error) {
|
|
6618
|
+
toolOutputs.push({
|
|
6619
|
+
call_id: output.call_id ?? output.id ?? "",
|
|
6620
|
+
type: "function_call_output",
|
|
6621
|
+
output: error instanceof Error ? error.message : "Unknown error",
|
|
6622
|
+
status: "incomplete"
|
|
6623
|
+
});
|
|
6624
|
+
}
|
|
6625
|
+
}
|
|
6626
|
+
}
|
|
6627
|
+
return toolOutputs;
|
|
6628
|
+
}
|
|
6629
|
+
async function handleOpenAIResponse(client, response, options) {
|
|
6630
|
+
let providerTokens = options?.providerTokens;
|
|
6631
|
+
if (!providerTokens) {
|
|
6632
|
+
try {
|
|
6633
|
+
providerTokens = await getProviderTokens();
|
|
6634
|
+
} catch {}
|
|
6635
|
+
}
|
|
6636
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6637
|
+
const functionCalls = response.output.filter((output) => output.type === "function_call");
|
|
6638
|
+
if (functionCalls.length === 0) {
|
|
6639
|
+
return response;
|
|
6640
|
+
}
|
|
6641
|
+
return handleOpenAIToolCalls(client, functionCalls, finalOptions);
|
|
6642
|
+
}
|
|
6643
|
+
// src/ai/anthropic.ts
|
|
6644
|
+
function convertMCPToolToAnthropic(mcpTool, _client, _options) {
|
|
6645
|
+
return {
|
|
6646
|
+
name: mcpTool.name,
|
|
6647
|
+
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
6648
|
+
input_schema: mcpTool.inputSchema || {
|
|
6649
|
+
type: "object",
|
|
6650
|
+
properties: {},
|
|
6651
|
+
required: []
|
|
6652
|
+
}
|
|
6653
|
+
};
|
|
6654
|
+
}
|
|
6655
|
+
function convertMCPToolsToAnthropic(client, options) {
|
|
6656
|
+
const mcpTools = client.getEnabledTools();
|
|
6657
|
+
return mcpTools.map((mcpTool) => convertMCPToolToAnthropic(mcpTool, client, options));
|
|
6658
|
+
}
|
|
6659
|
+
async function executeAnthropicToolCall(client, toolUse, options) {
|
|
6660
|
+
const result = await executeToolWithToken(client, toolUse.name, toolUse.input, options);
|
|
6661
|
+
return JSON.stringify(result);
|
|
6662
|
+
}
|
|
6663
|
+
async function handleAnthropicToolCalls(client, messageContent, options) {
|
|
6664
|
+
const toolResults = [];
|
|
6665
|
+
const toolUseBlocks = messageContent.filter((block) => block.type === "tool_use" && ("id" in block) && ("name" in block) && ("input" in block));
|
|
6666
|
+
for (const toolUse of toolUseBlocks) {
|
|
6667
|
+
try {
|
|
6668
|
+
const result = await executeAnthropicToolCall(client, toolUse, options);
|
|
6669
|
+
toolResults.push({
|
|
6670
|
+
type: "tool_result",
|
|
6671
|
+
tool_use_id: toolUse.id,
|
|
6672
|
+
content: result
|
|
6673
|
+
});
|
|
6674
|
+
} catch (error) {
|
|
6675
|
+
toolResults.push({
|
|
6676
|
+
type: "tool_result",
|
|
6677
|
+
tool_use_id: toolUse.id,
|
|
6678
|
+
content: JSON.stringify({
|
|
6679
|
+
error: error instanceof Error ? error.message : "Unknown error"
|
|
6680
|
+
})
|
|
6681
|
+
});
|
|
6682
|
+
}
|
|
6683
|
+
}
|
|
6684
|
+
return toolResults;
|
|
6685
|
+
}
|
|
6686
|
+
async function getAnthropicTools(client, options) {
|
|
6687
|
+
await ensureClientConnected(client);
|
|
6688
|
+
let providerTokens = options?.providerTokens;
|
|
6689
|
+
if (!providerTokens) {
|
|
6690
|
+
try {
|
|
6691
|
+
providerTokens = await getProviderTokens();
|
|
6692
|
+
} catch {}
|
|
6693
|
+
}
|
|
6694
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6695
|
+
return convertMCPToolsToAnthropic(client, finalOptions);
|
|
6696
|
+
}
|
|
6697
|
+
async function handleAnthropicMessage(client, message, options) {
|
|
6698
|
+
let providerTokens = options?.providerTokens;
|
|
6699
|
+
if (!providerTokens) {
|
|
6700
|
+
try {
|
|
6701
|
+
providerTokens = await getProviderTokens();
|
|
6702
|
+
} catch {}
|
|
7031
6703
|
}
|
|
7032
|
-
|
|
7033
|
-
|
|
7034
|
-
|
|
7035
|
-
|
|
7036
|
-
|
|
7037
|
-
|
|
7038
|
-
|
|
7039
|
-
|
|
7040
|
-
|
|
6704
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6705
|
+
const toolResults = await handleAnthropicToolCalls(client, message.content, finalOptions);
|
|
6706
|
+
if (toolResults.length === 0) {
|
|
6707
|
+
return message;
|
|
6708
|
+
}
|
|
6709
|
+
return [
|
|
6710
|
+
{
|
|
6711
|
+
role: message.role,
|
|
6712
|
+
content: message.content
|
|
6713
|
+
},
|
|
6714
|
+
{
|
|
6715
|
+
role: "user",
|
|
6716
|
+
content: toolResults
|
|
7041
6717
|
}
|
|
7042
|
-
|
|
6718
|
+
];
|
|
6719
|
+
}
|
|
6720
|
+
// src/ai/google.ts
|
|
6721
|
+
async function getGoogleType() {
|
|
6722
|
+
try {
|
|
6723
|
+
const dynamicImport = new Function("specifier", "return import(specifier)");
|
|
6724
|
+
const packageName = "@" + "google" + "/" + "genai";
|
|
6725
|
+
const googleGenAI = await dynamicImport(packageName);
|
|
6726
|
+
return googleGenAI.Type;
|
|
6727
|
+
} catch (error) {
|
|
6728
|
+
throw new Error("The @google/genai package is required to use Google AI integration. Install it with: npm install @google/genai");
|
|
7043
6729
|
}
|
|
7044
|
-
return exports_external.object({});
|
|
7045
6730
|
}
|
|
7046
|
-
|
|
7047
|
-
|
|
7048
|
-
|
|
7049
|
-
|
|
7050
|
-
|
|
7051
|
-
|
|
7052
|
-
|
|
7053
|
-
|
|
7054
|
-
|
|
7055
|
-
|
|
7056
|
-
|
|
7057
|
-
|
|
7058
|
-
|
|
7059
|
-
|
|
7060
|
-
|
|
7061
|
-
|
|
7062
|
-
|
|
7063
|
-
|
|
7064
|
-
|
|
7065
|
-
|
|
7066
|
-
|
|
6731
|
+
function convertJsonSchemaTypeToGoogleType(type, TypeEnum) {
|
|
6732
|
+
const typeMap = {
|
|
6733
|
+
string: TypeEnum.STRING,
|
|
6734
|
+
number: TypeEnum.NUMBER,
|
|
6735
|
+
integer: TypeEnum.INTEGER,
|
|
6736
|
+
boolean: TypeEnum.BOOLEAN,
|
|
6737
|
+
array: TypeEnum.ARRAY,
|
|
6738
|
+
object: TypeEnum.OBJECT
|
|
6739
|
+
};
|
|
6740
|
+
return typeMap[type.toLowerCase()] || TypeEnum.STRING;
|
|
6741
|
+
}
|
|
6742
|
+
function convertPropertiesToSchema(properties, TypeEnum) {
|
|
6743
|
+
const result = {};
|
|
6744
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
6745
|
+
if (!value || typeof value !== "object") {
|
|
6746
|
+
result[key] = value;
|
|
6747
|
+
continue;
|
|
6748
|
+
}
|
|
6749
|
+
const schema = {
|
|
6750
|
+
description: value.description,
|
|
6751
|
+
enum: value.enum
|
|
6752
|
+
};
|
|
6753
|
+
if (value.type) {
|
|
6754
|
+
schema.type = convertJsonSchemaTypeToGoogleType(value.type, TypeEnum);
|
|
6755
|
+
}
|
|
6756
|
+
if (value.items) {
|
|
6757
|
+
schema.items = convertPropertiesToSchema({ items: value.items }, TypeEnum).items;
|
|
6758
|
+
}
|
|
6759
|
+
if (value.properties) {
|
|
6760
|
+
schema.properties = convertPropertiesToSchema(value.properties, TypeEnum);
|
|
6761
|
+
}
|
|
6762
|
+
for (const [k, v] of Object.entries(value)) {
|
|
6763
|
+
if (!["type", "description", "enum", "items", "properties"].includes(k)) {
|
|
6764
|
+
schema[k] = v;
|
|
7067
6765
|
}
|
|
7068
6766
|
}
|
|
6767
|
+
result[key] = schema;
|
|
7069
6768
|
}
|
|
7070
|
-
const result = await client._callToolByName(toolName, args);
|
|
7071
6769
|
return result;
|
|
7072
6770
|
}
|
|
7073
|
-
async function
|
|
7074
|
-
|
|
7075
|
-
|
|
6771
|
+
async function convertMCPToolToGoogle(mcpTool, _client, _options) {
|
|
6772
|
+
const TypeEnum = await getGoogleType();
|
|
6773
|
+
const properties = mcpTool.inputSchema?.properties || {};
|
|
6774
|
+
const convertedProperties = convertPropertiesToSchema(properties, TypeEnum);
|
|
6775
|
+
const parameters = {
|
|
6776
|
+
type: TypeEnum.OBJECT,
|
|
6777
|
+
description: mcpTool.description || "",
|
|
6778
|
+
properties: convertedProperties
|
|
6779
|
+
};
|
|
6780
|
+
if (mcpTool.inputSchema?.required && mcpTool.inputSchema.required.length > 0) {
|
|
6781
|
+
parameters.required = mcpTool.inputSchema.required;
|
|
7076
6782
|
}
|
|
7077
|
-
}
|
|
7078
|
-
|
|
7079
|
-
// src/ai/vercel-ai.ts
|
|
7080
|
-
function convertMCPToolToVercelAI(mcpTool, client, options) {
|
|
7081
6783
|
return {
|
|
6784
|
+
name: mcpTool.name,
|
|
7082
6785
|
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
7083
|
-
|
|
7084
|
-
execute: async (args) => {
|
|
7085
|
-
if (options?.providerTokens) {
|
|
7086
|
-
return await executeToolWithToken(client, mcpTool.name, args, options);
|
|
7087
|
-
}
|
|
7088
|
-
return await client._callToolByName(mcpTool.name, args, options?.context ? { context: options.context } : undefined);
|
|
7089
|
-
}
|
|
6786
|
+
parameters
|
|
7090
6787
|
};
|
|
7091
6788
|
}
|
|
7092
|
-
function
|
|
6789
|
+
async function convertMCPToolsToGoogle(client, options) {
|
|
7093
6790
|
const mcpTools = client.getEnabledTools();
|
|
7094
|
-
|
|
7095
|
-
|
|
7096
|
-
|
|
6791
|
+
return await Promise.all(mcpTools.map((mcpTool) => convertMCPToolToGoogle(mcpTool, client, options)));
|
|
6792
|
+
}
|
|
6793
|
+
async function executeGoogleFunctionCall(client, functionCall, options) {
|
|
6794
|
+
if (!functionCall?.name) {
|
|
6795
|
+
throw new Error("Function call must have a name");
|
|
7097
6796
|
}
|
|
7098
|
-
|
|
6797
|
+
let providerTokens = options?.providerTokens;
|
|
6798
|
+
if (!providerTokens) {
|
|
6799
|
+
try {
|
|
6800
|
+
providerTokens = await getProviderTokens();
|
|
6801
|
+
} catch {}
|
|
6802
|
+
}
|
|
6803
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6804
|
+
const args = functionCall.args || {};
|
|
6805
|
+
const result = await executeToolWithToken(client, functionCall.name, args, finalOptions);
|
|
6806
|
+
return JSON.stringify(result);
|
|
7099
6807
|
}
|
|
7100
|
-
async function
|
|
6808
|
+
async function executeGoogleFunctionCalls(client, functionCalls, options) {
|
|
6809
|
+
if (!functionCalls || functionCalls.length === 0) {
|
|
6810
|
+
return [];
|
|
6811
|
+
}
|
|
6812
|
+
let providerTokens = options?.providerTokens;
|
|
6813
|
+
if (!providerTokens) {
|
|
6814
|
+
try {
|
|
6815
|
+
providerTokens = await getProviderTokens();
|
|
6816
|
+
} catch {}
|
|
6817
|
+
}
|
|
6818
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6819
|
+
const results = await Promise.all(functionCalls.map((call) => executeGoogleFunctionCall(client, call, finalOptions)));
|
|
6820
|
+
return results;
|
|
6821
|
+
}
|
|
6822
|
+
async function getGoogleTools(client, options) {
|
|
7101
6823
|
await ensureClientConnected(client);
|
|
7102
6824
|
let providerTokens = options?.providerTokens;
|
|
7103
6825
|
if (!providerTokens) {
|
|
@@ -7106,29 +6828,37 @@ async function getVercelAITools(client, options) {
|
|
|
7106
6828
|
} catch {}
|
|
7107
6829
|
}
|
|
7108
6830
|
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
7109
|
-
return
|
|
6831
|
+
return convertMCPToolsToGoogle(client, finalOptions);
|
|
7110
6832
|
}
|
|
7111
|
-
// src/ai/
|
|
7112
|
-
function
|
|
7113
|
-
const inputParams = mcpTool.inputSchema;
|
|
6833
|
+
// src/ai/cloudflare.ts
|
|
6834
|
+
function convertMCPToolToCloudflare(mcpTool, _client, _options) {
|
|
7114
6835
|
return {
|
|
7115
6836
|
type: "function",
|
|
7116
|
-
|
|
7117
|
-
|
|
7118
|
-
|
|
7119
|
-
|
|
6837
|
+
function: {
|
|
6838
|
+
name: mcpTool.name,
|
|
6839
|
+
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
6840
|
+
parameters: {
|
|
6841
|
+
type: "object",
|
|
6842
|
+
properties: mcpTool.inputSchema?.properties || {},
|
|
6843
|
+
required: mcpTool.inputSchema?.required || []
|
|
6844
|
+
}
|
|
6845
|
+
}
|
|
7120
6846
|
};
|
|
7121
6847
|
}
|
|
7122
|
-
function
|
|
6848
|
+
function convertMCPToolsToCloudflare(client, options) {
|
|
7123
6849
|
const mcpTools = client.getEnabledTools();
|
|
7124
|
-
|
|
6850
|
+
const tools = {};
|
|
6851
|
+
for (const mcpTool of mcpTools) {
|
|
6852
|
+
tools[mcpTool.name] = convertMCPToolToCloudflare(mcpTool, client, options);
|
|
6853
|
+
}
|
|
6854
|
+
return tools;
|
|
7125
6855
|
}
|
|
7126
|
-
async function
|
|
7127
|
-
const args = JSON.parse(toolCall.arguments);
|
|
6856
|
+
async function executeCloudflareToolCall(client, toolCall, options) {
|
|
6857
|
+
const args = typeof toolCall.arguments === "string" ? JSON.parse(toolCall.arguments) : toolCall.arguments;
|
|
7128
6858
|
const result = await executeToolWithToken(client, toolCall.name, args, options);
|
|
7129
6859
|
return JSON.stringify(result);
|
|
7130
6860
|
}
|
|
7131
|
-
async function
|
|
6861
|
+
async function getCloudflareTools(client, options) {
|
|
7132
6862
|
await ensureClientConnected(client);
|
|
7133
6863
|
let providerTokens = options?.providerTokens;
|
|
7134
6864
|
if (!providerTokens) {
|
|
@@ -7137,29 +6867,84 @@ async function getOpenAITools(client, options) {
|
|
|
7137
6867
|
} catch {}
|
|
7138
6868
|
}
|
|
7139
6869
|
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
7140
|
-
return
|
|
6870
|
+
return convertMCPToolsToCloudflare(client, finalOptions);
|
|
7141
6871
|
}
|
|
7142
|
-
// src/ai/
|
|
7143
|
-
function
|
|
6872
|
+
// src/ai/langchain.ts
|
|
6873
|
+
function convertMCPToolToLangChain(mcpTool, client, options) {
|
|
7144
6874
|
return {
|
|
7145
6875
|
name: mcpTool.name,
|
|
7146
6876
|
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
7147
|
-
|
|
7148
|
-
|
|
7149
|
-
|
|
7150
|
-
|
|
6877
|
+
schema: jsonSchemaToZod(mcpTool.inputSchema),
|
|
6878
|
+
func: async (...args) => {
|
|
6879
|
+
const input = args[0];
|
|
6880
|
+
const result = await executeToolWithToken(client, mcpTool.name, input, options);
|
|
6881
|
+
return JSON.stringify(result);
|
|
6882
|
+
}
|
|
6883
|
+
};
|
|
6884
|
+
}
|
|
6885
|
+
function convertMCPToolsToLangChain(client, options) {
|
|
6886
|
+
const mcpTools = client.getEnabledTools();
|
|
6887
|
+
return mcpTools.map((mcpTool) => convertMCPToolToLangChain(mcpTool, client, options));
|
|
6888
|
+
}
|
|
6889
|
+
async function getLangChainTools(client, options) {
|
|
6890
|
+
await ensureClientConnected(client);
|
|
6891
|
+
let providerTokens = options?.providerTokens;
|
|
6892
|
+
if (!providerTokens) {
|
|
6893
|
+
try {
|
|
6894
|
+
providerTokens = await getProviderTokens();
|
|
6895
|
+
} catch {}
|
|
6896
|
+
}
|
|
6897
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6898
|
+
return convertMCPToolsToLangChain(client, finalOptions);
|
|
6899
|
+
}
|
|
6900
|
+
// src/ai/llamaindex.ts
|
|
6901
|
+
function convertMCPToolToLlamaIndex(mcpTool, client, options) {
|
|
6902
|
+
return {
|
|
6903
|
+
name: mcpTool.name,
|
|
6904
|
+
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
6905
|
+
parameters: jsonSchemaToZod(mcpTool.inputSchema),
|
|
6906
|
+
execute: async (input) => {
|
|
6907
|
+
const result = await executeToolWithToken(client, mcpTool.name, input, options);
|
|
6908
|
+
return JSON.stringify(result);
|
|
6909
|
+
}
|
|
6910
|
+
};
|
|
6911
|
+
}
|
|
6912
|
+
function convertMCPToolsToLlamaIndex(client, options) {
|
|
6913
|
+
const mcpTools = client.getEnabledTools();
|
|
6914
|
+
return mcpTools.map((mcpTool) => convertMCPToolToLlamaIndex(mcpTool, client, options));
|
|
6915
|
+
}
|
|
6916
|
+
async function getLlamaIndexTools(client, options) {
|
|
6917
|
+
await ensureClientConnected(client);
|
|
6918
|
+
let providerTokens = options?.providerTokens;
|
|
6919
|
+
if (!providerTokens) {
|
|
6920
|
+
try {
|
|
6921
|
+
providerTokens = await getProviderTokens();
|
|
6922
|
+
} catch {}
|
|
6923
|
+
}
|
|
6924
|
+
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
6925
|
+
return convertMCPToolsToLlamaIndex(client, finalOptions);
|
|
6926
|
+
}
|
|
6927
|
+
// src/ai/mastra.ts
|
|
6928
|
+
function convertMCPToolToMastra(mcpTool, client, options) {
|
|
6929
|
+
return {
|
|
6930
|
+
id: mcpTool.name,
|
|
6931
|
+
description: mcpTool.description || `Execute ${mcpTool.name}`,
|
|
6932
|
+
inputSchema: jsonSchemaToZod(mcpTool.inputSchema),
|
|
6933
|
+
execute: async ({ context }) => {
|
|
6934
|
+
const result = await executeToolWithToken(client, mcpTool.name, context, options);
|
|
6935
|
+
return result;
|
|
7151
6936
|
}
|
|
7152
6937
|
};
|
|
7153
6938
|
}
|
|
7154
|
-
function
|
|
6939
|
+
function convertMCPToolsToMastra(client, options) {
|
|
7155
6940
|
const mcpTools = client.getEnabledTools();
|
|
7156
|
-
|
|
7157
|
-
|
|
7158
|
-
|
|
7159
|
-
|
|
7160
|
-
return
|
|
6941
|
+
const tools = {};
|
|
6942
|
+
for (const mcpTool of mcpTools) {
|
|
6943
|
+
tools[mcpTool.name] = convertMCPToolToMastra(mcpTool, client, options);
|
|
6944
|
+
}
|
|
6945
|
+
return tools;
|
|
7161
6946
|
}
|
|
7162
|
-
async function
|
|
6947
|
+
async function getMastraTools(client, options) {
|
|
7163
6948
|
await ensureClientConnected(client);
|
|
7164
6949
|
let providerTokens = options?.providerTokens;
|
|
7165
6950
|
if (!providerTokens) {
|
|
@@ -7168,269 +6953,573 @@ async function getAnthropicTools(client, options) {
|
|
|
7168
6953
|
} catch {}
|
|
7169
6954
|
}
|
|
7170
6955
|
const finalOptions = providerTokens ? { ...options, providerTokens } : options;
|
|
7171
|
-
return
|
|
6956
|
+
return convertMCPToolsToMastra(client, finalOptions);
|
|
7172
6957
|
}
|
|
7173
|
-
// src/ai/
|
|
7174
|
-
async function
|
|
7175
|
-
|
|
7176
|
-
|
|
7177
|
-
|
|
7178
|
-
|
|
7179
|
-
|
|
7180
|
-
|
|
7181
|
-
|
|
6958
|
+
// src/ai/index.ts
|
|
6959
|
+
async function getAITools(client, provider, options) {
|
|
6960
|
+
switch (provider) {
|
|
6961
|
+
case "vercel-ai":
|
|
6962
|
+
return await getVercelAITools(client, options);
|
|
6963
|
+
case "openai":
|
|
6964
|
+
return await getOpenAITools(client, options);
|
|
6965
|
+
case "anthropic":
|
|
6966
|
+
return await getAnthropicTools(client, options);
|
|
6967
|
+
case "google":
|
|
6968
|
+
return await getGoogleTools(client, options);
|
|
6969
|
+
case "cloudflare":
|
|
6970
|
+
return await getCloudflareTools(client, options);
|
|
6971
|
+
case "langchain":
|
|
6972
|
+
return await getLangChainTools(client, options);
|
|
6973
|
+
case "llamaindex":
|
|
6974
|
+
return await getLlamaIndexTools(client, options);
|
|
6975
|
+
case "mastra":
|
|
6976
|
+
return await getMastraTools(client, options);
|
|
6977
|
+
default:
|
|
6978
|
+
throw new Error(`Unsupported AI provider: ${provider}`);
|
|
7182
6979
|
}
|
|
7183
6980
|
}
|
|
7184
|
-
|
|
7185
|
-
|
|
7186
|
-
|
|
7187
|
-
|
|
7188
|
-
|
|
7189
|
-
|
|
7190
|
-
|
|
7191
|
-
|
|
7192
|
-
|
|
7193
|
-
|
|
6981
|
+
|
|
6982
|
+
// src/server.ts
|
|
6983
|
+
var globalServerConfig = null;
|
|
6984
|
+
function getDefaultRedirectUri() {
|
|
6985
|
+
if (typeof window !== "undefined") {
|
|
6986
|
+
return `${window.location.origin}/api/integrate/oauth/callback`;
|
|
6987
|
+
}
|
|
6988
|
+
const integrateUrl = getEnv("INTEGRATE_URL");
|
|
6989
|
+
if (integrateUrl) {
|
|
6990
|
+
return `${integrateUrl}/api/integrate/oauth/callback`;
|
|
6991
|
+
}
|
|
6992
|
+
const vercelUrl = getEnv("VERCEL_URL");
|
|
6993
|
+
if (vercelUrl) {
|
|
6994
|
+
return `https://${vercelUrl}/api/integrate/oauth/callback`;
|
|
6995
|
+
}
|
|
6996
|
+
return "http://localhost:3000/api/integrate/oauth/callback";
|
|
7194
6997
|
}
|
|
7195
|
-
function
|
|
7196
|
-
|
|
7197
|
-
|
|
7198
|
-
|
|
7199
|
-
|
|
7200
|
-
|
|
6998
|
+
function createMCPServer(config) {
|
|
6999
|
+
if (typeof window !== "undefined") {
|
|
7000
|
+
throw new Error("createMCPServer() should only be called on the server-side. " + "Use createMCPClient() for client-side code.");
|
|
7001
|
+
}
|
|
7002
|
+
const providers = {};
|
|
7003
|
+
const updatedIntegrations = config.integrations.map((integration) => {
|
|
7004
|
+
if (integration.oauth) {
|
|
7005
|
+
const { clientId, clientSecret, redirectUri: integrationRedirectUri, config: oauthConfig } = integration.oauth;
|
|
7006
|
+
if (!clientId || !clientSecret) {
|
|
7007
|
+
console.warn(`Warning: Integration "${integration.id}" is missing OAuth credentials. ` + `Provide clientId and clientSecret in the integration configuration.`);
|
|
7008
|
+
return integration;
|
|
7009
|
+
}
|
|
7010
|
+
const redirectUri = integrationRedirectUri || config.redirectUri || getDefaultRedirectUri();
|
|
7011
|
+
providers[integration.id] = {
|
|
7012
|
+
clientId,
|
|
7013
|
+
clientSecret,
|
|
7014
|
+
redirectUri,
|
|
7015
|
+
config: oauthConfig
|
|
7016
|
+
};
|
|
7017
|
+
return {
|
|
7018
|
+
...integration,
|
|
7019
|
+
oauth: {
|
|
7020
|
+
...integration.oauth,
|
|
7021
|
+
redirectUri
|
|
7022
|
+
}
|
|
7023
|
+
};
|
|
7201
7024
|
}
|
|
7202
|
-
|
|
7203
|
-
|
|
7204
|
-
|
|
7205
|
-
|
|
7206
|
-
|
|
7207
|
-
|
|
7025
|
+
return integration;
|
|
7026
|
+
});
|
|
7027
|
+
globalServerConfig = {
|
|
7028
|
+
providers,
|
|
7029
|
+
serverUrl: config.serverUrl,
|
|
7030
|
+
apiKey: config.apiKey
|
|
7031
|
+
};
|
|
7032
|
+
const clientConfig = {
|
|
7033
|
+
...config,
|
|
7034
|
+
integrations: updatedIntegrations,
|
|
7035
|
+
connectionMode: config.connectionMode || "lazy",
|
|
7036
|
+
singleton: config.singleton ?? true
|
|
7037
|
+
};
|
|
7038
|
+
const client = new MCPClientBase(clientConfig);
|
|
7039
|
+
if (config.apiKey) {
|
|
7040
|
+
client.setRequestHeader("X-API-KEY", config.apiKey);
|
|
7041
|
+
}
|
|
7042
|
+
client.__oauthConfig = {
|
|
7043
|
+
providers,
|
|
7044
|
+
serverUrl: config.serverUrl,
|
|
7045
|
+
apiKey: config.apiKey,
|
|
7046
|
+
getSessionContext: config.getSessionContext,
|
|
7047
|
+
setProviderToken: config.setProviderToken
|
|
7048
|
+
};
|
|
7049
|
+
const { POST, GET } = createOAuthRouteHandlers({
|
|
7050
|
+
providers,
|
|
7051
|
+
serverUrl: config.serverUrl,
|
|
7052
|
+
apiKey: config.apiKey,
|
|
7053
|
+
getSessionContext: config.getSessionContext,
|
|
7054
|
+
setProviderToken: config.setProviderToken
|
|
7055
|
+
});
|
|
7056
|
+
const handler = async (request, context) => {
|
|
7057
|
+
const method = request.method.toUpperCase();
|
|
7058
|
+
let action;
|
|
7059
|
+
let segments = [];
|
|
7060
|
+
if (context?.params?.action) {
|
|
7061
|
+
action = context.params.action;
|
|
7062
|
+
} else if (context?.params?.all) {
|
|
7063
|
+
const all = context.params.all;
|
|
7064
|
+
if (Array.isArray(all)) {
|
|
7065
|
+
segments = all;
|
|
7066
|
+
} else if (typeof all === "string") {
|
|
7067
|
+
segments = all.split("/").filter(Boolean);
|
|
7068
|
+
}
|
|
7069
|
+
if (segments.length === 2 && segments[0] === "oauth") {
|
|
7070
|
+
action = segments[1];
|
|
7071
|
+
} else if (segments.length === 1) {
|
|
7072
|
+
if (segments[0] === "mcp") {
|
|
7073
|
+
action = "mcp";
|
|
7074
|
+
} else {
|
|
7075
|
+
action = segments[0];
|
|
7076
|
+
}
|
|
7077
|
+
} else if (segments.length > 0) {
|
|
7078
|
+
action = segments[segments.length - 1];
|
|
7079
|
+
}
|
|
7080
|
+
} else {
|
|
7081
|
+
const url = new URL(request.url);
|
|
7082
|
+
const pathParts = url.pathname.split("/").filter(Boolean);
|
|
7083
|
+
segments = pathParts;
|
|
7084
|
+
const oauthIndex = pathParts.indexOf("oauth");
|
|
7085
|
+
if (oauthIndex >= 0 && oauthIndex < pathParts.length - 1) {
|
|
7086
|
+
action = pathParts[oauthIndex + 1];
|
|
7087
|
+
} else if (pathParts.length > 0) {
|
|
7088
|
+
action = pathParts[pathParts.length - 1];
|
|
7089
|
+
} else {
|
|
7090
|
+
action = "callback";
|
|
7091
|
+
}
|
|
7208
7092
|
}
|
|
7209
|
-
if (
|
|
7210
|
-
|
|
7093
|
+
if (action === "mcp" && method === "POST") {
|
|
7094
|
+
try {
|
|
7095
|
+
const body = await request.json();
|
|
7096
|
+
const authHeader = request.headers.get("authorization");
|
|
7097
|
+
const { OAuthHandler: OAuthHandler2 } = await Promise.resolve().then(() => exports_base_handler);
|
|
7098
|
+
const oauthHandler = new OAuthHandler2({
|
|
7099
|
+
providers,
|
|
7100
|
+
serverUrl: config.serverUrl,
|
|
7101
|
+
apiKey: config.apiKey
|
|
7102
|
+
});
|
|
7103
|
+
const result = await oauthHandler.handleToolCall(body, authHeader);
|
|
7104
|
+
return Response.json(result);
|
|
7105
|
+
} catch (error) {
|
|
7106
|
+
console.error("[MCP Tool Call] Error:", error);
|
|
7107
|
+
return Response.json({ error: error.message || "Failed to execute tool call" }, { status: error.statusCode || 500 });
|
|
7108
|
+
}
|
|
7211
7109
|
}
|
|
7212
|
-
if (
|
|
7213
|
-
|
|
7110
|
+
if (segments.length > 0) {
|
|
7111
|
+
if (segments.length === 2 && segments[0] !== "oauth") {
|
|
7112
|
+
return Response.json({ error: `Invalid route: /${segments.join("/")}` }, { status: 404 });
|
|
7113
|
+
}
|
|
7114
|
+
if (segments.length === 1 && segments[0] === "mcp") {
|
|
7115
|
+
return Response.json({ error: `Method ${method} not allowed for /mcp route. Use POST.` }, { status: 405 });
|
|
7116
|
+
}
|
|
7214
7117
|
}
|
|
7215
|
-
|
|
7216
|
-
|
|
7217
|
-
|
|
7118
|
+
if (method === "GET" && action === "callback") {
|
|
7119
|
+
const url = new URL(request.url);
|
|
7120
|
+
const searchParams = url.searchParams;
|
|
7121
|
+
const code = searchParams.get("code");
|
|
7122
|
+
const state = searchParams.get("state");
|
|
7123
|
+
const error = searchParams.get("error");
|
|
7124
|
+
const errorDescription = searchParams.get("error_description");
|
|
7125
|
+
const defaultRedirectUrl = "/";
|
|
7126
|
+
const errorRedirectUrl = "/auth-error";
|
|
7127
|
+
if (error) {
|
|
7128
|
+
const errorMsg = errorDescription || error;
|
|
7129
|
+
console.error("[OAuth Redirect] Error:", errorMsg);
|
|
7130
|
+
return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent(errorMsg)}`, request.url));
|
|
7218
7131
|
}
|
|
7132
|
+
if (!code || !state) {
|
|
7133
|
+
console.error("[OAuth Redirect] Missing code or state parameter");
|
|
7134
|
+
return Response.redirect(new URL(`${errorRedirectUrl}?error=${encodeURIComponent("Invalid OAuth callback")}`, request.url));
|
|
7135
|
+
}
|
|
7136
|
+
let returnUrl = defaultRedirectUrl;
|
|
7137
|
+
try {
|
|
7138
|
+
const { parseState: parseState2 } = await Promise.resolve().then(() => exports_pkce);
|
|
7139
|
+
const stateData = parseState2(state);
|
|
7140
|
+
if (stateData.returnUrl) {
|
|
7141
|
+
returnUrl = stateData.returnUrl;
|
|
7142
|
+
}
|
|
7143
|
+
} catch (e) {
|
|
7144
|
+
try {
|
|
7145
|
+
const referrer = request.headers.get("referer") || request.headers.get("referrer");
|
|
7146
|
+
if (referrer) {
|
|
7147
|
+
const referrerUrl = new URL(referrer);
|
|
7148
|
+
const currentUrl = new URL(request.url);
|
|
7149
|
+
if (referrerUrl.origin === currentUrl.origin) {
|
|
7150
|
+
returnUrl = referrerUrl.pathname + referrerUrl.search;
|
|
7151
|
+
}
|
|
7152
|
+
}
|
|
7153
|
+
} catch {}
|
|
7154
|
+
}
|
|
7155
|
+
const targetUrl = new URL(returnUrl, request.url);
|
|
7156
|
+
targetUrl.hash = `oauth_callback=${encodeURIComponent(JSON.stringify({ code, state }))}`;
|
|
7157
|
+
return Response.redirect(targetUrl);
|
|
7158
|
+
}
|
|
7159
|
+
const handlerContext = { params: { action: action || "callback" } };
|
|
7160
|
+
if (method === "POST") {
|
|
7161
|
+
return POST(request, handlerContext);
|
|
7162
|
+
} else if (method === "GET") {
|
|
7163
|
+
return GET(request, handlerContext);
|
|
7164
|
+
} else {
|
|
7165
|
+
return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
|
|
7219
7166
|
}
|
|
7220
|
-
result[key] = schema;
|
|
7221
|
-
}
|
|
7222
|
-
return result;
|
|
7223
|
-
}
|
|
7224
|
-
async function convertMCPToolToGoogle(mcpTool, _client, _options) {
|
|
7225
|
-
const TypeEnum = await getGoogleType();
|
|
7226
|
-
const properties = mcpTool.inputSchema?.properties || {};
|
|
7227
|
-
const convertedProperties = convertPropertiesToSchema(properties, TypeEnum);
|
|
7228
|
-
const parameters = {
|
|
7229
|
-
type: TypeEnum.OBJECT,
|
|
7230
|
-
description: mcpTool.description || "",
|
|
7231
|
-
properties: convertedProperties
|
|
7232
7167
|
};
|
|
7233
|
-
|
|
7234
|
-
|
|
7235
|
-
|
|
7168
|
+
const serverClient = client;
|
|
7169
|
+
serverClient.handler = handler;
|
|
7170
|
+
serverClient.POST = POST;
|
|
7171
|
+
serverClient.GET = GET;
|
|
7236
7172
|
return {
|
|
7237
|
-
|
|
7238
|
-
|
|
7239
|
-
|
|
7173
|
+
client: serverClient,
|
|
7174
|
+
POST,
|
|
7175
|
+
GET
|
|
7240
7176
|
};
|
|
7241
7177
|
}
|
|
7242
|
-
|
|
7243
|
-
const
|
|
7244
|
-
return
|
|
7178
|
+
function createOAuthRouteHandlers(config) {
|
|
7179
|
+
const handler = createNextOAuthHandler(config);
|
|
7180
|
+
return handler.createRoutes();
|
|
7245
7181
|
}
|
|
7246
|
-
async
|
|
7247
|
-
if (!
|
|
7248
|
-
|
|
7182
|
+
var POST = async (req, context) => {
|
|
7183
|
+
if (!globalServerConfig) {
|
|
7184
|
+
return Response.json({ error: "OAuth not configured. Call createMCPServer() in your server initialization file first." }, { status: 500 });
|
|
7249
7185
|
}
|
|
7250
|
-
|
|
7251
|
-
|
|
7252
|
-
|
|
7253
|
-
|
|
7254
|
-
|
|
7186
|
+
const handler = createNextOAuthHandler(globalServerConfig);
|
|
7187
|
+
const routes = handler.createRoutes();
|
|
7188
|
+
return routes.POST(req, context);
|
|
7189
|
+
};
|
|
7190
|
+
var GET = async (req, context) => {
|
|
7191
|
+
if (!globalServerConfig) {
|
|
7192
|
+
return Response.json({ error: "OAuth not configured. Call createMCPServer() in your server initialization file first." }, { status: 500 });
|
|
7255
7193
|
}
|
|
7256
|
-
const
|
|
7257
|
-
const
|
|
7258
|
-
|
|
7259
|
-
|
|
7260
|
-
|
|
7261
|
-
|
|
7262
|
-
|
|
7263
|
-
|
|
7194
|
+
const handler = createNextOAuthHandler(globalServerConfig);
|
|
7195
|
+
const routes = handler.createRoutes();
|
|
7196
|
+
return routes.GET(req, context);
|
|
7197
|
+
};
|
|
7198
|
+
function toNextJsHandler(clientOrOptions, redirectOptions) {
|
|
7199
|
+
let config;
|
|
7200
|
+
let redirectUrl;
|
|
7201
|
+
let errorRedirectUrl;
|
|
7202
|
+
if (!clientOrOptions) {
|
|
7203
|
+
config = globalServerConfig;
|
|
7204
|
+
redirectUrl = redirectOptions?.redirectUrl;
|
|
7205
|
+
errorRedirectUrl = redirectOptions?.errorRedirectUrl;
|
|
7206
|
+
} else if (clientOrOptions.__oauthConfig) {
|
|
7207
|
+
config = clientOrOptions.__oauthConfig;
|
|
7208
|
+
redirectUrl = redirectOptions?.redirectUrl;
|
|
7209
|
+
errorRedirectUrl = redirectOptions?.errorRedirectUrl;
|
|
7210
|
+
} else if (typeof clientOrOptions === "object" && clientOrOptions.providers) {
|
|
7211
|
+
const options = clientOrOptions;
|
|
7212
|
+
config = {
|
|
7213
|
+
providers: options.providers,
|
|
7214
|
+
serverUrl: options.serverUrl,
|
|
7215
|
+
apiKey: options.apiKey,
|
|
7216
|
+
getSessionContext: options.getSessionContext,
|
|
7217
|
+
setProviderToken: options.setProviderToken
|
|
7218
|
+
};
|
|
7219
|
+
redirectUrl = options.redirectUrl;
|
|
7220
|
+
errorRedirectUrl = options.errorRedirectUrl;
|
|
7221
|
+
} else {
|
|
7222
|
+
config = null;
|
|
7264
7223
|
}
|
|
7265
|
-
|
|
7266
|
-
|
|
7267
|
-
|
|
7268
|
-
|
|
7269
|
-
|
|
7224
|
+
if (!config) {
|
|
7225
|
+
throw new Error(`toNextJsHandler requires either:
|
|
7226
|
+
1. A client instance from createMCPServer()
|
|
7227
|
+
2. A config object with providers property
|
|
7228
|
+
3. No arguments (uses global config from createMCPServer)`);
|
|
7270
7229
|
}
|
|
7271
|
-
const
|
|
7272
|
-
|
|
7273
|
-
|
|
7230
|
+
const POST2 = async (req, context) => {
|
|
7231
|
+
const handler = createNextOAuthHandler(config);
|
|
7232
|
+
const routes = handler.toNextJsHandler({
|
|
7233
|
+
redirectUrl,
|
|
7234
|
+
errorRedirectUrl
|
|
7235
|
+
});
|
|
7236
|
+
return routes.POST(req, context);
|
|
7237
|
+
};
|
|
7238
|
+
const GET2 = async (req, context) => {
|
|
7239
|
+
const handler = createNextOAuthHandler(config);
|
|
7240
|
+
const routes = handler.toNextJsHandler({
|
|
7241
|
+
redirectUrl,
|
|
7242
|
+
errorRedirectUrl
|
|
7243
|
+
});
|
|
7244
|
+
return routes.GET(req, context);
|
|
7245
|
+
};
|
|
7246
|
+
return { POST: POST2, GET: GET2 };
|
|
7274
7247
|
}
|
|
7275
|
-
|
|
7276
|
-
|
|
7277
|
-
|
|
7278
|
-
|
|
7279
|
-
|
|
7280
|
-
|
|
7281
|
-
|
|
7248
|
+
function toSolidStartHandler(clientOrHandlerOrOptions, _redirectOptions) {
|
|
7249
|
+
if (clientOrHandlerOrOptions && clientOrHandlerOrOptions.handler && typeof clientOrHandlerOrOptions.handler === "function") {
|
|
7250
|
+
const baseHandler = clientOrHandlerOrOptions.handler;
|
|
7251
|
+
const handler2 = async (event) => {
|
|
7252
|
+
return baseHandler(event.request);
|
|
7253
|
+
};
|
|
7254
|
+
return {
|
|
7255
|
+
GET: handler2,
|
|
7256
|
+
POST: handler2,
|
|
7257
|
+
PATCH: handler2,
|
|
7258
|
+
PUT: handler2,
|
|
7259
|
+
DELETE: handler2
|
|
7260
|
+
};
|
|
7282
7261
|
}
|
|
7283
|
-
|
|
7284
|
-
|
|
7285
|
-
|
|
7286
|
-
|
|
7287
|
-
|
|
7288
|
-
|
|
7289
|
-
|
|
7290
|
-
|
|
7291
|
-
|
|
7292
|
-
|
|
7293
|
-
|
|
7294
|
-
|
|
7295
|
-
|
|
7296
|
-
|
|
7297
|
-
|
|
7262
|
+
if (typeof clientOrHandlerOrOptions === "function") {
|
|
7263
|
+
const baseHandler = clientOrHandlerOrOptions;
|
|
7264
|
+
const handler2 = async (event) => {
|
|
7265
|
+
return baseHandler(event.request);
|
|
7266
|
+
};
|
|
7267
|
+
return {
|
|
7268
|
+
GET: handler2,
|
|
7269
|
+
POST: handler2,
|
|
7270
|
+
PATCH: handler2,
|
|
7271
|
+
PUT: handler2,
|
|
7272
|
+
DELETE: handler2
|
|
7273
|
+
};
|
|
7274
|
+
}
|
|
7275
|
+
const options = clientOrHandlerOrOptions;
|
|
7276
|
+
if (!options.providers || Object.keys(options.providers).length === 0) {
|
|
7277
|
+
throw new Error("toSolidStartHandler requires either a handler function or a providers config object");
|
|
7278
|
+
}
|
|
7279
|
+
const { providers, serverUrl, apiKey, redirectUrl, errorRedirectUrl } = options;
|
|
7280
|
+
const nextHandler = createNextOAuthHandler({
|
|
7281
|
+
providers,
|
|
7282
|
+
serverUrl,
|
|
7283
|
+
apiKey
|
|
7284
|
+
});
|
|
7285
|
+
const routes = nextHandler.toNextJsHandler({
|
|
7286
|
+
redirectUrl: redirectUrl || "/",
|
|
7287
|
+
errorRedirectUrl: errorRedirectUrl || "/auth-error"
|
|
7288
|
+
});
|
|
7289
|
+
const handler = async (event) => {
|
|
7290
|
+
const method = event.request.method.toUpperCase();
|
|
7291
|
+
const url = new URL(event.request.url);
|
|
7292
|
+
const pathParts = url.pathname.split("/").filter(Boolean);
|
|
7293
|
+
const integrateIndex = pathParts.indexOf("integrate");
|
|
7294
|
+
const segments = integrateIndex >= 0 ? pathParts.slice(integrateIndex + 1) : [];
|
|
7295
|
+
const context = {
|
|
7296
|
+
params: segments
|
|
7297
|
+
};
|
|
7298
|
+
if (method === "POST") {
|
|
7299
|
+
return routes.POST(event.request, { params: { all: context.params } });
|
|
7300
|
+
} else if (method === "GET") {
|
|
7301
|
+
return routes.GET(event.request, { params: { all: context.params } });
|
|
7302
|
+
} else {
|
|
7303
|
+
return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
|
|
7298
7304
|
}
|
|
7299
7305
|
};
|
|
7306
|
+
return {
|
|
7307
|
+
GET: handler,
|
|
7308
|
+
POST: handler,
|
|
7309
|
+
PATCH: handler,
|
|
7310
|
+
PUT: handler,
|
|
7311
|
+
DELETE: handler
|
|
7312
|
+
};
|
|
7300
7313
|
}
|
|
7301
|
-
function
|
|
7302
|
-
|
|
7303
|
-
|
|
7304
|
-
|
|
7305
|
-
|
|
7314
|
+
function toSvelteKitHandler(clientOrHandlerOrOptions, _redirectOptions) {
|
|
7315
|
+
if (clientOrHandlerOrOptions && clientOrHandlerOrOptions.handler && typeof clientOrHandlerOrOptions.handler === "function") {
|
|
7316
|
+
const baseHandler = clientOrHandlerOrOptions.handler;
|
|
7317
|
+
return async (event) => {
|
|
7318
|
+
const all = event.params?.all;
|
|
7319
|
+
return baseHandler(event.request, { params: { all } });
|
|
7320
|
+
};
|
|
7306
7321
|
}
|
|
7307
|
-
|
|
7308
|
-
|
|
7309
|
-
async
|
|
7310
|
-
|
|
7311
|
-
|
|
7312
|
-
|
|
7313
|
-
}
|
|
7314
|
-
async function getCloudflareTools(client, options) {
|
|
7315
|
-
await ensureClientConnected(client);
|
|
7316
|
-
let providerTokens = options?.providerTokens;
|
|
7317
|
-
if (!providerTokens) {
|
|
7318
|
-
try {
|
|
7319
|
-
providerTokens = await getProviderTokens();
|
|
7320
|
-
} catch {}
|
|
7322
|
+
if (typeof clientOrHandlerOrOptions === "function") {
|
|
7323
|
+
const baseHandler = clientOrHandlerOrOptions;
|
|
7324
|
+
return async (event) => {
|
|
7325
|
+
const all = event.params?.all;
|
|
7326
|
+
return baseHandler(event.request, { params: { all } });
|
|
7327
|
+
};
|
|
7321
7328
|
}
|
|
7322
|
-
const
|
|
7323
|
-
|
|
7324
|
-
|
|
7325
|
-
|
|
7326
|
-
|
|
7327
|
-
|
|
7328
|
-
|
|
7329
|
-
|
|
7330
|
-
|
|
7331
|
-
|
|
7332
|
-
|
|
7333
|
-
|
|
7334
|
-
|
|
7329
|
+
const options = clientOrHandlerOrOptions;
|
|
7330
|
+
if (!options.providers || Object.keys(options.providers).length === 0) {
|
|
7331
|
+
throw new Error("toSvelteKitHandler requires either a handler function or a providers config object");
|
|
7332
|
+
}
|
|
7333
|
+
const { providers, serverUrl, apiKey, redirectUrl, errorRedirectUrl } = options;
|
|
7334
|
+
const nextHandler = createNextOAuthHandler({
|
|
7335
|
+
providers,
|
|
7336
|
+
serverUrl,
|
|
7337
|
+
apiKey
|
|
7338
|
+
});
|
|
7339
|
+
const routes = nextHandler.toNextJsHandler({
|
|
7340
|
+
redirectUrl: redirectUrl || "/",
|
|
7341
|
+
errorRedirectUrl: errorRedirectUrl || "/auth-error"
|
|
7342
|
+
});
|
|
7343
|
+
return async (event) => {
|
|
7344
|
+
const method = event.request.method.toUpperCase();
|
|
7345
|
+
const all = event.params?.all;
|
|
7346
|
+
const context = {
|
|
7347
|
+
params: Array.isArray(all) ? all : all ? all.split("/").filter(Boolean) : []
|
|
7348
|
+
};
|
|
7349
|
+
if (method === "POST") {
|
|
7350
|
+
return routes.POST(event.request, { params: { all: context.params } });
|
|
7351
|
+
} else if (method === "GET") {
|
|
7352
|
+
return routes.GET(event.request, { params: { all: context.params } });
|
|
7353
|
+
} else {
|
|
7354
|
+
return Response.json({ error: `Method ${method} not allowed` }, { status: 405 });
|
|
7335
7355
|
}
|
|
7336
7356
|
};
|
|
7337
7357
|
}
|
|
7338
|
-
|
|
7339
|
-
|
|
7340
|
-
|
|
7341
|
-
|
|
7342
|
-
|
|
7343
|
-
|
|
7344
|
-
|
|
7345
|
-
|
|
7346
|
-
|
|
7347
|
-
|
|
7348
|
-
}
|
|
7358
|
+
// src/adapters/node.ts
|
|
7359
|
+
function fromNodeHeaders(nodeHeaders) {
|
|
7360
|
+
const webHeaders = new Headers;
|
|
7361
|
+
for (const [key, value] of Object.entries(nodeHeaders)) {
|
|
7362
|
+
if (value !== undefined) {
|
|
7363
|
+
if (Array.isArray(value)) {
|
|
7364
|
+
value.forEach((v) => webHeaders.append(key, v));
|
|
7365
|
+
} else {
|
|
7366
|
+
webHeaders.set(key, value);
|
|
7367
|
+
}
|
|
7368
|
+
}
|
|
7349
7369
|
}
|
|
7350
|
-
|
|
7351
|
-
return convertMCPToolsToLangChain(client, finalOptions);
|
|
7370
|
+
return webHeaders;
|
|
7352
7371
|
}
|
|
7353
|
-
|
|
7354
|
-
|
|
7355
|
-
|
|
7356
|
-
|
|
7357
|
-
|
|
7358
|
-
|
|
7359
|
-
|
|
7360
|
-
|
|
7361
|
-
|
|
7362
|
-
|
|
7363
|
-
|
|
7372
|
+
async function toWebRequest(req) {
|
|
7373
|
+
const protocol = req.socket.encrypted ? "https" : "http";
|
|
7374
|
+
const host = req.headers.host || "localhost";
|
|
7375
|
+
const url = `${protocol}://${host}${req.url}`;
|
|
7376
|
+
const headers = fromNodeHeaders(req.headers);
|
|
7377
|
+
let body;
|
|
7378
|
+
if (req.method && ["POST", "PUT", "PATCH"].includes(req.method)) {
|
|
7379
|
+
body = await new Promise((resolve, reject) => {
|
|
7380
|
+
let data = "";
|
|
7381
|
+
req.on("data", (chunk) => data += chunk);
|
|
7382
|
+
req.on("end", () => resolve(data));
|
|
7383
|
+
req.on("error", reject);
|
|
7384
|
+
});
|
|
7385
|
+
}
|
|
7386
|
+
return new Request(url, {
|
|
7387
|
+
method: req.method,
|
|
7388
|
+
headers,
|
|
7389
|
+
body: body || undefined
|
|
7390
|
+
});
|
|
7364
7391
|
}
|
|
7365
|
-
function
|
|
7366
|
-
|
|
7367
|
-
|
|
7392
|
+
async function sendWebResponse(webRes, nodeRes) {
|
|
7393
|
+
nodeRes.statusCode = webRes.status;
|
|
7394
|
+
webRes.headers.forEach((value, key) => {
|
|
7395
|
+
nodeRes.setHeader(key, value);
|
|
7396
|
+
});
|
|
7397
|
+
const body = await webRes.text();
|
|
7398
|
+
nodeRes.end(body);
|
|
7368
7399
|
}
|
|
7369
|
-
|
|
7370
|
-
|
|
7371
|
-
|
|
7372
|
-
if (!providerTokens) {
|
|
7400
|
+
function toNodeHandler(config) {
|
|
7401
|
+
const oauthHandler = new OAuthHandler(config);
|
|
7402
|
+
return async (req, res) => {
|
|
7373
7403
|
try {
|
|
7374
|
-
|
|
7375
|
-
|
|
7376
|
-
|
|
7377
|
-
|
|
7378
|
-
|
|
7379
|
-
|
|
7380
|
-
|
|
7381
|
-
|
|
7382
|
-
|
|
7383
|
-
|
|
7384
|
-
|
|
7385
|
-
|
|
7386
|
-
|
|
7387
|
-
|
|
7388
|
-
|
|
7404
|
+
const webReq = await toWebRequest(req);
|
|
7405
|
+
const url = new URL(webReq.url);
|
|
7406
|
+
const segments = url.pathname.split("/").filter(Boolean);
|
|
7407
|
+
const action = segments[segments.length - 1];
|
|
7408
|
+
let webRes;
|
|
7409
|
+
if (req.method === "POST") {
|
|
7410
|
+
if (action === "authorize") {
|
|
7411
|
+
const result = await oauthHandler.handleAuthorize(webReq);
|
|
7412
|
+
const headers = { "Content-Type": "application/json" };
|
|
7413
|
+
if (result.setCookie) {
|
|
7414
|
+
headers["Set-Cookie"] = result.setCookie;
|
|
7415
|
+
}
|
|
7416
|
+
webRes = new Response(JSON.stringify(result), {
|
|
7417
|
+
status: 200,
|
|
7418
|
+
headers
|
|
7419
|
+
});
|
|
7420
|
+
} else if (action === "callback") {
|
|
7421
|
+
const result = await oauthHandler.handleCallback(webReq);
|
|
7422
|
+
const headers = { "Content-Type": "application/json" };
|
|
7423
|
+
if (result.clearCookie) {
|
|
7424
|
+
headers["Set-Cookie"] = result.clearCookie;
|
|
7425
|
+
}
|
|
7426
|
+
webRes = new Response(JSON.stringify(result), {
|
|
7427
|
+
status: 200,
|
|
7428
|
+
headers
|
|
7429
|
+
});
|
|
7430
|
+
} else if (action === "disconnect") {
|
|
7431
|
+
const authHeader = webReq.headers.get("authorization");
|
|
7432
|
+
if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
7433
|
+
webRes = new Response(JSON.stringify({ error: "Missing or invalid Authorization header" }), {
|
|
7434
|
+
status: 400,
|
|
7435
|
+
headers: { "Content-Type": "application/json" }
|
|
7436
|
+
});
|
|
7437
|
+
} else {
|
|
7438
|
+
const accessToken = authHeader.substring(7);
|
|
7439
|
+
const body = await webReq.json();
|
|
7440
|
+
const { provider } = body;
|
|
7441
|
+
if (!provider) {
|
|
7442
|
+
webRes = new Response(JSON.stringify({ error: "Missing provider in request body" }), {
|
|
7443
|
+
status: 400,
|
|
7444
|
+
headers: { "Content-Type": "application/json" }
|
|
7445
|
+
});
|
|
7446
|
+
} else {
|
|
7447
|
+
const result = await oauthHandler.handleDisconnect({ provider }, accessToken);
|
|
7448
|
+
webRes = new Response(JSON.stringify(result), {
|
|
7449
|
+
status: 200,
|
|
7450
|
+
headers: { "Content-Type": "application/json" }
|
|
7451
|
+
});
|
|
7452
|
+
}
|
|
7453
|
+
}
|
|
7454
|
+
} else {
|
|
7455
|
+
webRes = new Response(JSON.stringify({ error: `Unknown action: ${action}` }), {
|
|
7456
|
+
status: 404,
|
|
7457
|
+
headers: { "Content-Type": "application/json" }
|
|
7458
|
+
});
|
|
7459
|
+
}
|
|
7460
|
+
} else if (req.method === "GET" && action === "status") {
|
|
7461
|
+
const provider = url.searchParams.get("provider");
|
|
7462
|
+
const authHeader = webReq.headers.get("authorization");
|
|
7463
|
+
if (!provider) {
|
|
7464
|
+
webRes = new Response(JSON.stringify({ error: "Missing provider query parameter" }), {
|
|
7465
|
+
status: 400,
|
|
7466
|
+
headers: { "Content-Type": "application/json" }
|
|
7467
|
+
});
|
|
7468
|
+
} else if (!authHeader || !authHeader.startsWith("Bearer ")) {
|
|
7469
|
+
webRes = new Response(JSON.stringify({ error: "Missing or invalid Authorization header" }), {
|
|
7470
|
+
status: 400,
|
|
7471
|
+
headers: { "Content-Type": "application/json" }
|
|
7472
|
+
});
|
|
7473
|
+
} else {
|
|
7474
|
+
const accessToken = authHeader.substring(7);
|
|
7475
|
+
const result = await oauthHandler.handleStatus(provider, accessToken);
|
|
7476
|
+
webRes = new Response(JSON.stringify(result), {
|
|
7477
|
+
status: 200,
|
|
7478
|
+
headers: { "Content-Type": "application/json" }
|
|
7479
|
+
});
|
|
7480
|
+
}
|
|
7481
|
+
} else {
|
|
7482
|
+
webRes = new Response(JSON.stringify({ error: `Unknown action: ${action}` }), {
|
|
7483
|
+
status: 404,
|
|
7484
|
+
headers: { "Content-Type": "application/json" }
|
|
7485
|
+
});
|
|
7486
|
+
}
|
|
7487
|
+
await sendWebResponse(webRes, res);
|
|
7488
|
+
} catch (error) {
|
|
7489
|
+
console.error("[OAuth Handler] Error:", error);
|
|
7490
|
+
const errorRes = new Response(JSON.stringify({ error: error.message || "Internal server error" }), {
|
|
7491
|
+
status: 500,
|
|
7492
|
+
headers: { "Content-Type": "application/json" }
|
|
7493
|
+
});
|
|
7494
|
+
await sendWebResponse(errorRes, res);
|
|
7389
7495
|
}
|
|
7390
7496
|
};
|
|
7391
7497
|
}
|
|
7392
|
-
|
|
7393
|
-
|
|
7394
|
-
|
|
7395
|
-
|
|
7396
|
-
|
|
7397
|
-
|
|
7398
|
-
|
|
7399
|
-
}
|
|
7400
|
-
|
|
7401
|
-
|
|
7402
|
-
|
|
7403
|
-
if (!providerTokens) {
|
|
7404
|
-
try {
|
|
7405
|
-
providerTokens = await getProviderTokens();
|
|
7406
|
-
} catch {}
|
|
7498
|
+
// src/adapters/svelte-kit.ts
|
|
7499
|
+
async function svelteKitHandler({
|
|
7500
|
+
authConfig,
|
|
7501
|
+
event,
|
|
7502
|
+
resolve,
|
|
7503
|
+
basePath = "/api/integrate"
|
|
7504
|
+
}) {
|
|
7505
|
+
const { url } = event;
|
|
7506
|
+
const baseUrl = new URL(basePath, url.origin);
|
|
7507
|
+
if (!url.pathname.startsWith(baseUrl.pathname)) {
|
|
7508
|
+
return resolve(event);
|
|
7407
7509
|
}
|
|
7408
|
-
|
|
7409
|
-
return convertMCPToolsToMastra(client, finalOptions);
|
|
7510
|
+
return authConfig(event.request);
|
|
7410
7511
|
}
|
|
7411
|
-
// src/
|
|
7412
|
-
|
|
7413
|
-
|
|
7414
|
-
|
|
7415
|
-
|
|
7416
|
-
|
|
7417
|
-
|
|
7418
|
-
|
|
7419
|
-
|
|
7420
|
-
case "google":
|
|
7421
|
-
return await getGoogleTools(client, options);
|
|
7422
|
-
case "cloudflare":
|
|
7423
|
-
return await getCloudflareTools(client, options);
|
|
7424
|
-
case "langchain":
|
|
7425
|
-
return await getLangChainTools(client, options);
|
|
7426
|
-
case "llamaindex":
|
|
7427
|
-
return await getLlamaIndexTools(client, options);
|
|
7428
|
-
case "mastra":
|
|
7429
|
-
return await getMastraTools(client, options);
|
|
7430
|
-
default:
|
|
7431
|
-
throw new Error(`Unsupported AI provider: ${provider}`);
|
|
7432
|
-
}
|
|
7512
|
+
// src/adapters/tanstack-start.ts
|
|
7513
|
+
function toTanStackStartHandler(handler) {
|
|
7514
|
+
const baseHandler = async ({ request }) => {
|
|
7515
|
+
return handler(request);
|
|
7516
|
+
};
|
|
7517
|
+
return {
|
|
7518
|
+
GET: baseHandler,
|
|
7519
|
+
POST: baseHandler
|
|
7520
|
+
};
|
|
7433
7521
|
}
|
|
7522
|
+
var createTanStackOAuthHandler = toTanStackStartHandler;
|
|
7434
7523
|
export {
|
|
7435
7524
|
toTanStackStartHandler,
|
|
7436
7525
|
toSvelteKitHandler,
|
|
@@ -7439,6 +7528,10 @@ export {
|
|
|
7439
7528
|
toNextJsHandler,
|
|
7440
7529
|
svelteKitHandler,
|
|
7441
7530
|
notionIntegration,
|
|
7531
|
+
handleOpenAIToolCalls,
|
|
7532
|
+
handleOpenAIResponse,
|
|
7533
|
+
handleAnthropicToolCalls,
|
|
7534
|
+
handleAnthropicMessage,
|
|
7442
7535
|
gmailIntegration,
|
|
7443
7536
|
githubIntegration,
|
|
7444
7537
|
getVercelAITools,
|