integrate-sdk 0.7.0 → 0.7.1

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/index.js CHANGED
@@ -244,6 +244,176 @@ function base64UrlDecode(str) {
244
244
  }
245
245
  }
246
246
 
247
+ // src/adapters/base-handler.ts
248
+ var exports_base_handler = {};
249
+ __export(exports_base_handler, {
250
+ OAuthHandler: () => OAuthHandler
251
+ });
252
+
253
+ class OAuthHandler {
254
+ config;
255
+ serverUrl;
256
+ apiKey;
257
+ constructor(config) {
258
+ this.config = config;
259
+ if (!config || !config.providers) {
260
+ throw new Error("OAuthHandler requires a valid config with providers");
261
+ }
262
+ this.serverUrl = config.serverUrl || MCP_SERVER_URL2;
263
+ this.apiKey = config.apiKey;
264
+ }
265
+ getHeaders(additionalHeaders) {
266
+ const headers = {
267
+ ...additionalHeaders
268
+ };
269
+ if (this.apiKey) {
270
+ headers["X-API-KEY"] = this.apiKey;
271
+ }
272
+ return headers;
273
+ }
274
+ async handleAuthorize(request) {
275
+ const providerConfig = this.config.providers[request.provider];
276
+ if (!providerConfig) {
277
+ throw new Error(`Provider ${request.provider} not configured. Add OAuth credentials to your API route configuration.`);
278
+ }
279
+ if (!providerConfig.clientId || !providerConfig.clientSecret) {
280
+ throw new Error(`Missing OAuth credentials for ${request.provider}. Check your environment variables.`);
281
+ }
282
+ const url = new URL("/oauth/authorize", this.serverUrl);
283
+ url.searchParams.set("provider", request.provider);
284
+ url.searchParams.set("client_id", providerConfig.clientId);
285
+ url.searchParams.set("client_secret", providerConfig.clientSecret);
286
+ url.searchParams.set("scope", request.scopes.join(","));
287
+ url.searchParams.set("state", request.state);
288
+ url.searchParams.set("code_challenge", request.codeChallenge);
289
+ url.searchParams.set("code_challenge_method", request.codeChallengeMethod);
290
+ const redirectUri = request.redirectUri || providerConfig.redirectUri;
291
+ if (redirectUri) {
292
+ url.searchParams.set("redirect_uri", redirectUri);
293
+ }
294
+ const response = await fetch(url.toString(), {
295
+ method: "GET",
296
+ headers: this.getHeaders()
297
+ });
298
+ if (!response.ok) {
299
+ const error = await response.text();
300
+ throw new Error(`MCP server failed to generate authorization URL: ${error}`);
301
+ }
302
+ const data = await response.json();
303
+ return data;
304
+ }
305
+ async handleCallback(request) {
306
+ const providerConfig = this.config.providers[request.provider];
307
+ if (!providerConfig) {
308
+ throw new Error(`Provider ${request.provider} not configured. Add OAuth credentials to your API route configuration.`);
309
+ }
310
+ if (!providerConfig.clientId || !providerConfig.clientSecret) {
311
+ throw new Error(`Missing OAuth credentials for ${request.provider}. Check your environment variables.`);
312
+ }
313
+ const url = new URL("/oauth/callback", this.serverUrl);
314
+ const response = await fetch(url.toString(), {
315
+ method: "POST",
316
+ headers: this.getHeaders({
317
+ "Content-Type": "application/json"
318
+ }),
319
+ body: JSON.stringify({
320
+ provider: request.provider,
321
+ code: request.code,
322
+ code_verifier: request.codeVerifier,
323
+ state: request.state,
324
+ client_id: providerConfig.clientId,
325
+ client_secret: providerConfig.clientSecret,
326
+ redirect_uri: providerConfig.redirectUri
327
+ })
328
+ });
329
+ if (!response.ok) {
330
+ const error = await response.text();
331
+ throw new Error(`MCP server failed to exchange authorization code: ${error}`);
332
+ }
333
+ const data = await response.json();
334
+ return data;
335
+ }
336
+ async handleStatus(provider, accessToken) {
337
+ const url = new URL("/oauth/status", this.serverUrl);
338
+ url.searchParams.set("provider", provider);
339
+ const response = await fetch(url.toString(), {
340
+ method: "GET",
341
+ headers: this.getHeaders({
342
+ Authorization: `Bearer ${accessToken}`
343
+ })
344
+ });
345
+ if (!response.ok) {
346
+ if (response.status === 401) {
347
+ return {
348
+ authorized: false
349
+ };
350
+ }
351
+ const error = await response.text();
352
+ throw new Error(`MCP server failed to check authorization status: ${error}`);
353
+ }
354
+ const data = await response.json();
355
+ return data;
356
+ }
357
+ async handleDisconnect(request, accessToken) {
358
+ if (!accessToken) {
359
+ throw new Error("No access token provided. Cannot disconnect provider.");
360
+ }
361
+ const url = new URL("/oauth/disconnect", this.serverUrl);
362
+ const response = await fetch(url.toString(), {
363
+ method: "POST",
364
+ headers: this.getHeaders({
365
+ "Content-Type": "application/json",
366
+ Authorization: `Bearer ${accessToken}`
367
+ }),
368
+ body: JSON.stringify({
369
+ provider: request.provider
370
+ })
371
+ });
372
+ if (!response.ok) {
373
+ const error = await response.text();
374
+ throw new Error(`MCP server failed to disconnect provider: ${error}`);
375
+ }
376
+ const data = await response.json();
377
+ return data;
378
+ }
379
+ async handleToolCall(request, authHeader) {
380
+ const url = new URL("/tools/call", this.serverUrl);
381
+ const headers = this.getHeaders({
382
+ "Content-Type": "application/json"
383
+ });
384
+ if (authHeader && authHeader.startsWith("Bearer ")) {
385
+ headers["Authorization"] = authHeader;
386
+ }
387
+ const jsonRpcRequest = {
388
+ jsonrpc: "2.0",
389
+ id: Date.now() + Math.random(),
390
+ method: "tools/call",
391
+ params: {
392
+ name: request.name,
393
+ arguments: request.arguments || {}
394
+ }
395
+ };
396
+ const response = await fetch(url.toString(), {
397
+ method: "POST",
398
+ headers,
399
+ body: JSON.stringify(jsonRpcRequest)
400
+ });
401
+ if (!response.ok) {
402
+ const error = await response.text();
403
+ throw new Error(`MCP server failed to execute tool call: ${error}`);
404
+ }
405
+ const jsonRpcResponse = await response.json();
406
+ if (jsonRpcResponse.error) {
407
+ const error = new Error(jsonRpcResponse.error.message || "Tool call failed");
408
+ error.code = jsonRpcResponse.error.code;
409
+ error.data = jsonRpcResponse.error.data;
410
+ throw error;
411
+ }
412
+ return jsonRpcResponse.result;
413
+ }
414
+ }
415
+ var MCP_SERVER_URL2 = "https://mcp.integrate.dev/api/v1/mcp";
416
+
247
417
  // src/protocol/jsonrpc.ts
248
418
  function parseMessage(message) {
249
419
  try {
@@ -1582,171 +1752,6 @@ async function clearClientCache() {
1582
1752
  }
1583
1753
  }));
1584
1754
  }
1585
- // src/adapters/base-handler.ts
1586
- var MCP_SERVER_URL2 = "https://mcp.integrate.dev/api/v1/mcp";
1587
-
1588
- class OAuthHandler {
1589
- config;
1590
- serverUrl;
1591
- apiKey;
1592
- constructor(config) {
1593
- this.config = config;
1594
- if (!config || !config.providers) {
1595
- throw new Error("OAuthHandler requires a valid config with providers");
1596
- }
1597
- this.serverUrl = config.serverUrl || MCP_SERVER_URL2;
1598
- this.apiKey = config.apiKey;
1599
- }
1600
- getHeaders(additionalHeaders) {
1601
- const headers = {
1602
- ...additionalHeaders
1603
- };
1604
- if (this.apiKey) {
1605
- headers["X-API-KEY"] = this.apiKey;
1606
- }
1607
- return headers;
1608
- }
1609
- async handleAuthorize(request) {
1610
- const providerConfig = this.config.providers[request.provider];
1611
- if (!providerConfig) {
1612
- throw new Error(`Provider ${request.provider} not configured. Add OAuth credentials to your API route configuration.`);
1613
- }
1614
- if (!providerConfig.clientId || !providerConfig.clientSecret) {
1615
- throw new Error(`Missing OAuth credentials for ${request.provider}. Check your environment variables.`);
1616
- }
1617
- const url = new URL("/oauth/authorize", this.serverUrl);
1618
- url.searchParams.set("provider", request.provider);
1619
- url.searchParams.set("client_id", providerConfig.clientId);
1620
- url.searchParams.set("client_secret", providerConfig.clientSecret);
1621
- url.searchParams.set("scope", request.scopes.join(","));
1622
- url.searchParams.set("state", request.state);
1623
- url.searchParams.set("code_challenge", request.codeChallenge);
1624
- url.searchParams.set("code_challenge_method", request.codeChallengeMethod);
1625
- const redirectUri = request.redirectUri || providerConfig.redirectUri;
1626
- if (redirectUri) {
1627
- url.searchParams.set("redirect_uri", redirectUri);
1628
- }
1629
- const response = await fetch(url.toString(), {
1630
- method: "GET",
1631
- headers: this.getHeaders()
1632
- });
1633
- if (!response.ok) {
1634
- const error = await response.text();
1635
- throw new Error(`MCP server failed to generate authorization URL: ${error}`);
1636
- }
1637
- const data = await response.json();
1638
- return data;
1639
- }
1640
- async handleCallback(request) {
1641
- const providerConfig = this.config.providers[request.provider];
1642
- if (!providerConfig) {
1643
- throw new Error(`Provider ${request.provider} not configured. Add OAuth credentials to your API route configuration.`);
1644
- }
1645
- if (!providerConfig.clientId || !providerConfig.clientSecret) {
1646
- throw new Error(`Missing OAuth credentials for ${request.provider}. Check your environment variables.`);
1647
- }
1648
- const url = new URL("/oauth/callback", this.serverUrl);
1649
- const response = await fetch(url.toString(), {
1650
- method: "POST",
1651
- headers: this.getHeaders({
1652
- "Content-Type": "application/json"
1653
- }),
1654
- body: JSON.stringify({
1655
- provider: request.provider,
1656
- code: request.code,
1657
- code_verifier: request.codeVerifier,
1658
- state: request.state,
1659
- client_id: providerConfig.clientId,
1660
- client_secret: providerConfig.clientSecret,
1661
- redirect_uri: providerConfig.redirectUri
1662
- })
1663
- });
1664
- if (!response.ok) {
1665
- const error = await response.text();
1666
- throw new Error(`MCP server failed to exchange authorization code: ${error}`);
1667
- }
1668
- const data = await response.json();
1669
- return data;
1670
- }
1671
- async handleStatus(provider, accessToken) {
1672
- const url = new URL("/oauth/status", this.serverUrl);
1673
- url.searchParams.set("provider", provider);
1674
- const response = await fetch(url.toString(), {
1675
- method: "GET",
1676
- headers: this.getHeaders({
1677
- Authorization: `Bearer ${accessToken}`
1678
- })
1679
- });
1680
- if (!response.ok) {
1681
- if (response.status === 401) {
1682
- return {
1683
- authorized: false
1684
- };
1685
- }
1686
- const error = await response.text();
1687
- throw new Error(`MCP server failed to check authorization status: ${error}`);
1688
- }
1689
- const data = await response.json();
1690
- return data;
1691
- }
1692
- async handleDisconnect(request, accessToken) {
1693
- if (!accessToken) {
1694
- throw new Error("No access token provided. Cannot disconnect provider.");
1695
- }
1696
- const url = new URL("/oauth/disconnect", this.serverUrl);
1697
- const response = await fetch(url.toString(), {
1698
- method: "POST",
1699
- headers: this.getHeaders({
1700
- "Content-Type": "application/json",
1701
- Authorization: `Bearer ${accessToken}`
1702
- }),
1703
- body: JSON.stringify({
1704
- provider: request.provider
1705
- })
1706
- });
1707
- if (!response.ok) {
1708
- const error = await response.text();
1709
- throw new Error(`MCP server failed to disconnect provider: ${error}`);
1710
- }
1711
- const data = await response.json();
1712
- return data;
1713
- }
1714
- async handleToolCall(request, authHeader) {
1715
- const url = new URL("/tools/call", this.serverUrl);
1716
- const headers = this.getHeaders({
1717
- "Content-Type": "application/json"
1718
- });
1719
- if (authHeader && authHeader.startsWith("Bearer ")) {
1720
- headers["Authorization"] = authHeader;
1721
- }
1722
- const jsonRpcRequest = {
1723
- jsonrpc: "2.0",
1724
- id: Date.now() + Math.random(),
1725
- method: "tools/call",
1726
- params: {
1727
- name: request.name,
1728
- arguments: request.arguments || {}
1729
- }
1730
- };
1731
- const response = await fetch(url.toString(), {
1732
- method: "POST",
1733
- headers,
1734
- body: JSON.stringify(jsonRpcRequest)
1735
- });
1736
- if (!response.ok) {
1737
- const error = await response.text();
1738
- throw new Error(`MCP server failed to execute tool call: ${error}`);
1739
- }
1740
- const jsonRpcResponse = await response.json();
1741
- if (jsonRpcResponse.error) {
1742
- const error = new Error(jsonRpcResponse.error.message || "Tool call failed");
1743
- error.code = jsonRpcResponse.error.code;
1744
- error.data = jsonRpcResponse.error.data;
1745
- throw error;
1746
- }
1747
- return jsonRpcResponse.result;
1748
- }
1749
- }
1750
1755
  // src/adapters/nextjs.ts
1751
1756
  function createNextOAuthHandler(config) {
1752
1757
  const handler = new OAuthHandler(config);
@@ -2338,7 +2343,23 @@ function createMCPServer(config) {
2338
2343
  if (segments.length === 2 && segments[0] !== "oauth") {
2339
2344
  return Response.json({ error: `Invalid route: /${segments.join("/")}` }, { status: 404 });
2340
2345
  }
2341
- if (segments.length === 1 && segments[0] === "mcp") {}
2346
+ if (segments.length === 1 && segments[0] === "mcp" && method === "POST") {
2347
+ try {
2348
+ const body = await request.json();
2349
+ const authHeader = request.headers.get("authorization");
2350
+ const { OAuthHandler: OAuthHandler2 } = await Promise.resolve().then(() => exports_base_handler);
2351
+ const oauthHandler = new OAuthHandler2({
2352
+ providers,
2353
+ serverUrl: config.serverUrl,
2354
+ apiKey: config.apiKey
2355
+ });
2356
+ const result = await oauthHandler.handleToolCall(body, authHeader);
2357
+ return Response.json(result);
2358
+ } catch (error) {
2359
+ console.error("[MCP Tool Call] Error:", error);
2360
+ return Response.json({ error: error.message || "Failed to execute tool call" }, { status: error.statusCode || 500 });
2361
+ }
2362
+ }
2342
2363
  }
2343
2364
  if (method === "GET" && action === "callback") {
2344
2365
  const url = new URL(request.url);
package/dist/oauth.js CHANGED
@@ -11,7 +11,10 @@ var __export = (target, all) => {
11
11
  var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
12
12
 
13
13
  // src/adapters/base-handler.ts
14
- var MCP_SERVER_URL = "https://mcp.integrate.dev/api/v1/mcp";
14
+ var exports_base_handler = {};
15
+ __export(exports_base_handler, {
16
+ OAuthHandler: () => OAuthHandler
17
+ });
15
18
 
16
19
  class OAuthHandler {
17
20
  config;
@@ -175,6 +178,7 @@ class OAuthHandler {
175
178
  return jsonRpcResponse.result;
176
179
  }
177
180
  }
181
+ var MCP_SERVER_URL = "https://mcp.integrate.dev/api/v1/mcp";
178
182
 
179
183
  // src/adapters/auto-routes.ts
180
184
  var globalOAuthConfig = null;