integrate-sdk 0.1.4 → 0.1.7

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/client.d.ts CHANGED
@@ -5,6 +5,7 @@
5
5
  import type { MCPTool, MCPToolCallResponse } from "./protocol/messages.js";
6
6
  import type { MCPPlugin, OAuthConfig } from "./plugins/types.js";
7
7
  import type { MCPClientConfig } from "./config/types.js";
8
+ import { type AuthenticationError } from "./errors.js";
8
9
  /**
9
10
  * Tool invocation options
10
11
  */
@@ -26,6 +27,9 @@ export declare class MCPClient<TPlugins extends readonly MCPPlugin[] = readonly
26
27
  private enabledToolNames;
27
28
  private initialized;
28
29
  private clientInfo;
30
+ private onReauthRequired?;
31
+ private maxReauthRetries;
32
+ private authState;
29
33
  constructor(config: MCPClientConfig<TPlugins>);
30
34
  /**
31
35
  * Initialize all plugins
@@ -44,9 +48,17 @@ export declare class MCPClient<TPlugins extends readonly MCPPlugin[] = readonly
44
48
  */
45
49
  private discoverTools;
46
50
  /**
47
- * Call a tool by name
51
+ * Call a tool by name with automatic retry on authentication failure
48
52
  */
49
53
  callTool(name: string, args?: Record<string, unknown>): Promise<MCPToolCallResponse>;
54
+ /**
55
+ * Internal method to call a tool with retry logic
56
+ */
57
+ private callToolWithRetry;
58
+ /**
59
+ * Get the OAuth provider for a given tool
60
+ */
61
+ private getProviderForTool;
50
62
  /**
51
63
  * Get a tool by name
52
64
  */
@@ -83,6 +95,22 @@ export declare class MCPClient<TPlugins extends readonly MCPPlugin[] = readonly
83
95
  * Check if client is initialized
84
96
  */
85
97
  isInitialized(): boolean;
98
+ /**
99
+ * Get authentication state for a specific provider
100
+ */
101
+ getAuthState(provider: string): {
102
+ authenticated: boolean;
103
+ lastError?: AuthenticationError;
104
+ } | undefined;
105
+ /**
106
+ * Check if a specific provider is authenticated
107
+ */
108
+ isProviderAuthenticated(provider: string): boolean;
109
+ /**
110
+ * Manually trigger re-authentication for a specific provider
111
+ * Useful if you want to proactively refresh tokens
112
+ */
113
+ reauthenticate(provider: string): Promise<boolean>;
86
114
  }
87
115
  /**
88
116
  * Create a new MCP Client instance
@@ -1 +1 @@
1
- {"version":3,"file":"client.d.ts","sourceRoot":"","sources":["../src/client.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,KAAK,EACV,OAAO,EAEP,mBAAmB,EAIpB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjE,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAOzD;;GAEG;AACH,MAAM,WAAW,qBAAqB;IACpC,gBAAgB;IAChB,IAAI,EAAE,MAAM,CAAC;IACb,qBAAqB;IACrB,SAAS,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CACrC;AAED;;;;GAIG;AACH,qBAAa,SAAS,CAAC,QAAQ,SAAS,SAAS,SAAS,EAAE,GAAG,SAAS,SAAS,EAAE;IACjF,OAAO,CAAC,SAAS,CAAuB;IACxC,OAAO,CAAC,OAAO,CAAW;IAC1B,OAAO,CAAC,cAAc,CAAmC;IACzD,OAAO,CAAC,gBAAgB,CAA0B;IAClD,OAAO,CAAC,WAAW,CAAS;IAC5B,OAAO,CAAC,UAAU,CAAoC;gBAE1C,MAAM,EAAE,eAAe,CAAC,QAAQ,CAAC;IAwB7C;;OAEG;YACW,iBAAiB;IAQ/B;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAyB9B;;OAEG;YACW,UAAU;IAkBxB;;OAEG;YACW,aAAa;IAoB3B;;OAEG;IACG,QAAQ,CACZ,IAAI,EAAE,MAAM,EACZ,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAC7B,OAAO,CAAC,mBAAmB,CAAC;IA8B/B;;OAEG;IACH,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,OAAO,GAAG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O;IAIxB;;OAEG;IACH,YAAY,CAAC,QAAQ,EAAE,MAAM,GAAG;QAAE,aAAa,EAAE,OAAO,CAAC;QAAC,SAAS,CAAC,EAAE,mBAAmB,CAAA;KAAE,GAAG,SAAS;IAIvG;;OAEG;IACH,uBAAuB,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAIlD;;;OAGG;IACG,cAAc,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,OAAO,CAAC;CA2BzD;AAED;;;;;;;;;;;;;;;;;;;;GAoBG;AACH,wBAAgB,eAAe,CAAC,QAAQ,SAAS,SAAS,SAAS,EAAE,EACnE,MAAM,EAAE,eAAe,CAAC,QAAQ,CAAC,GAChC,SAAS,CAAC,QAAQ,CAAC,CAErB"}
@@ -3,6 +3,24 @@
3
3
  * Type-safe configuration with inference
4
4
  */
5
5
  import type { MCPPlugin } from "../plugins/types.js";
6
+ import type { AuthenticationError } from "../errors.js";
7
+ /**
8
+ * Re-authentication context provided to the callback
9
+ */
10
+ export interface ReauthContext {
11
+ /** The plugin/provider that needs re-authentication */
12
+ provider: string;
13
+ /** The error that triggered re-authentication */
14
+ error: AuthenticationError;
15
+ /** The tool name that was being called (if applicable) */
16
+ toolName?: string;
17
+ }
18
+ /**
19
+ * Re-authentication handler function
20
+ * Called when authentication fails and user needs to re-authenticate
21
+ * Should return true if re-authentication was successful, false otherwise
22
+ */
23
+ export type ReauthHandler = (context: ReauthContext) => Promise<boolean> | boolean;
6
24
  /**
7
25
  * Main client configuration
8
26
  */
@@ -18,6 +36,33 @@ export interface MCPClientConfig<TPlugins extends readonly MCPPlugin[]> {
18
36
  name: string;
19
37
  version: string;
20
38
  };
39
+ /**
40
+ * Handler called when authentication fails and re-authentication is needed
41
+ * This is typically called when OAuth tokens expire or become invalid
42
+ *
43
+ * @param context - Information about the authentication failure
44
+ * @returns Promise<boolean> - true if re-authentication was successful, false otherwise
45
+ *
46
+ * @example
47
+ * ```typescript
48
+ * const client = createMCPClient({
49
+ * plugins: [githubPlugin(...)],
50
+ * onReauthRequired: async (context) => {
51
+ * console.log(`Re-auth needed for ${context.provider}`);
52
+ * // Trigger your OAuth flow here
53
+ * // Return true if successful, false otherwise
54
+ * return await triggerOAuthFlow(context.provider);
55
+ * }
56
+ * });
57
+ * ```
58
+ */
59
+ onReauthRequired?: ReauthHandler;
60
+ /**
61
+ * Maximum number of automatic retry attempts when authentication fails
62
+ * Default: 1 (one retry after re-authentication)
63
+ * Set to 0 to disable automatic retries
64
+ */
65
+ maxReauthRetries?: number;
21
66
  }
22
67
  /**
23
68
  * Helper type to infer enabled tools from plugins
@@ -1 +1 @@
1
- {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/config/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAC;AAErD;;GAEG;AACH,MAAM,WAAW,eAAe,CAAC,QAAQ,SAAS,SAAS,SAAS,EAAE;IACpE,iCAAiC;IACjC,OAAO,EAAE,QAAQ,CAAC;IAElB,mDAAmD;IACnD,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAEjC,uDAAuD;IACvD,OAAO,CAAC,EAAE,MAAM,CAAC;IAEjB,yBAAyB;IACzB,UAAU,CAAC,EAAE;QACX,IAAI,EAAE,MAAM,CAAC;QACb,OAAO,EAAE,MAAM,CAAC;KACjB,CAAC;CACH;AAED;;GAEG;AACH,MAAM,MAAM,iBAAiB,CAAC,QAAQ,SAAS,SAAS,SAAS,EAAE,IACjE,QAAQ,CAAC,MAAM,CAAC,CAAC,OAAO,CAAC,CAAC,MAAM,CAAC,CAAC;AAEpC;;GAEG;AACH,MAAM,MAAM,gBAAgB,CAAC,QAAQ,SAAS,SAAS,SAAS,EAAE,IAAI;KACnE,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC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,EAAE,IAAI;KACnE,CAAC,IAAI,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC,OAAO,CAAC;CAC/C,CAAC"}
@@ -0,0 +1,67 @@
1
+ /**
2
+ * Custom error types for the Integrate SDK
3
+ */
4
+ /**
5
+ * Base error class for all SDK errors
6
+ */
7
+ export declare class IntegrateSDKError extends Error {
8
+ constructor(message: string);
9
+ }
10
+ /**
11
+ * Error thrown when authentication fails or tokens are invalid
12
+ */
13
+ export declare class AuthenticationError extends IntegrateSDKError {
14
+ readonly statusCode?: number;
15
+ readonly provider?: string;
16
+ constructor(message: string, statusCode?: number, provider?: string);
17
+ }
18
+ /**
19
+ * Error thrown when access is forbidden (insufficient permissions)
20
+ */
21
+ export declare class AuthorizationError extends IntegrateSDKError {
22
+ readonly statusCode?: number;
23
+ readonly requiredScopes?: string[];
24
+ constructor(message: string, statusCode?: number, requiredScopes?: string[]);
25
+ }
26
+ /**
27
+ * Error thrown when OAuth tokens have expired and need to be refreshed
28
+ */
29
+ export declare class TokenExpiredError extends AuthenticationError {
30
+ constructor(message: string, provider?: string);
31
+ }
32
+ /**
33
+ * Error thrown when a connection to the server fails
34
+ */
35
+ export declare class ConnectionError extends IntegrateSDKError {
36
+ readonly statusCode?: number;
37
+ constructor(message: string, statusCode?: number);
38
+ }
39
+ /**
40
+ * Error thrown when a tool call fails
41
+ */
42
+ export declare class ToolCallError extends IntegrateSDKError {
43
+ readonly toolName: string;
44
+ readonly originalError?: unknown;
45
+ constructor(message: string, toolName: string, originalError?: unknown);
46
+ }
47
+ /**
48
+ * Helper function to determine if an error is an authentication error
49
+ */
50
+ export declare function isAuthError(error: unknown): error is AuthenticationError;
51
+ /**
52
+ * Helper function to determine if an error is a token expired error
53
+ */
54
+ export declare function isTokenExpiredError(error: unknown): error is TokenExpiredError;
55
+ /**
56
+ * Helper function to determine if an error is an authorization error
57
+ */
58
+ export declare function isAuthorizationError(error: unknown): error is AuthorizationError;
59
+ /**
60
+ * Helper function to parse error responses from the MCP server
61
+ * and convert them to appropriate error types
62
+ */
63
+ export declare function parseServerError(error: any, context?: {
64
+ toolName?: string;
65
+ provider?: string;
66
+ }): IntegrateSDKError;
67
+ //# sourceMappingURL=errors.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,KAAK;gBAC9B,OAAO,EAAE,MAAM;CAI5B;AAED;;GAEG;AACH,qBAAa,mBAAoB,SAAQ,iBAAiB;IACxD,SAAgB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpC,SAAgB,QAAQ,CAAC,EAAE,MAAM,CAAC;gBAEtB,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;CAMpE;AAED;;GAEG;AACH,qBAAa,kBAAmB,SAAQ,iBAAiB;IACvD,SAAgB,UAAU,CAAC,EAAE,MAAM,CAAC;IACpC,SAAgB,cAAc,CAAC,EAAE,MAAM,EAAE,CAAC;gBAE9B,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM,EAAE,cAAc,CAAC,EAAE,MAAM,EAAE;CAM5E;AAED;;GAEG;AACH,qBAAa,iBAAkB,SAAQ,mBAAmB;gBAC5C,OAAO,EAAE,MAAM,EAAE,QAAQ,CAAC,EAAE,MAAM;CAI/C;AAED;;GAEG;AACH,qBAAa,eAAgB,SAAQ,iBAAiB;IACpD,SAAgB,UAAU,CAAC,EAAE,MAAM,CAAC;gBAExB,OAAO,EAAE,MAAM,EAAE,UAAU,CAAC,EAAE,MAAM;CAKjD;AAED;;GAEG;AACH,qBAAa,aAAc,SAAQ,iBAAiB;IAClD,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,SAAgB,aAAa,CAAC,EAAE,OAAO,CAAC;gBAE5B,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,aAAa,CAAC,EAAE,OAAO;CAMvE;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,mBAAmB,CAExE;AAED;;GAEG;AACH,wBAAgB,mBAAmB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,iBAAiB,CAE9E;AAED;;GAEG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,KAAK,IAAI,kBAAkB,CAEhF;AAED;;;GAGG;AACH,wBAAgB,gBAAgB,CAC9B,KAAK,EAAE,GAAG,EACV,OAAO,CAAC,EAAE;IAAE,QAAQ,CAAC,EAAE,MAAM,CAAC;IAAC,QAAQ,CAAC,EAAE,MAAM,CAAA;CAAE,GACjD,iBAAiB,CA+FnB"}
package/dist/index.d.ts CHANGED
@@ -4,7 +4,8 @@
4
4
  */
5
5
  export { MCPClient, createMCPClient } from "./client.js";
6
6
  export type { ToolInvocationOptions } from "./client.js";
7
- export type { MCPClientConfig } from "./config/types.js";
7
+ export type { MCPClientConfig, ReauthContext, ReauthHandler } from "./config/types.js";
8
+ export { IntegrateSDKError, AuthenticationError, AuthorizationError, TokenExpiredError, ConnectionError, ToolCallError, isAuthError, isTokenExpiredError, isAuthorizationError, parseServerError, } from "./errors.js";
8
9
  export type { MCPPlugin, OAuthConfig, ExtractPluginIds, ExtractPluginTools, } from "./plugins/types.js";
9
10
  export { githubPlugin } from "./plugins/github.js";
10
11
  export type { GitHubPluginConfig, GitHubTools } from "./plugins/github.js";
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAGH,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACzD,YAAY,EAAE,qBAAqB,EAAE,MAAM,aAAa,CAAC;AAGzD,YAAY,EAAE,eAAe,EAAE,MAAM,mB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AAiB,EACjB,mBAAmB,EACnB,kBAAkB,EAClB,iBAAiB,EACjB,eAAe,EACf,aAAa,EACb,WAAW,EACX,mBAAmB,EACnB,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,aAAa,CAAC;AAGrB,YAAY,EACV,SAAS,EACT,WAAW,EACX,gBAAgB,EAChB,kBAAkB,GACnB,MAAM,oBAAoB,CAAC;AAG5B,OAAO,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AACnD,YAAY,EAAE,kBAAkB,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAE3E,OAAO,EAAE,WAAW,EAAE,MAAM,oBAAoB,CAAC;AACjD,YAAY,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,oBAAoB,CAAC;AAExE,OAAO,EACL,kBAAkB,EAClB,kBAAkB,GACnB,MAAM,sBAAsB,CAAC;AAC9B,YAAY,EAAE,wBAAwB,EAAE,MAAM,sBAAsB,CAAC;AAGrE,OAAO,EACL,wBAAwB,EACxB,yBAAyB,EACzB,gBAAgB,GACjB,MAAM,6BAA6B,CAAC;AACrC,YAAY,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAGhE,YAAY,EACV,cAAc,EACd,eAAe,EACf,sBAAsB,EACtB,oBAAoB,EACpB,mBAAmB,EACnB,OAAO,EACP,oBAAoB,EACpB,iBAAiB,EACjB,mBAAmB,EACnB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,wBAAwB,CAAC;AAEhC,OAAO,EAAE,SAAS,EAAE,MAAM,wBAAwB,CAAC;AAGnD,OAAO,EAAE,oBAAoB,EAAE,MAAM,6BAA6B,CAAC;AACnE,YAAY,EACV,cAAc,EACd,2BAA2B,GAC5B,MAAM,6BAA6B,CAAC"}
package/dist/index.js CHANGED
@@ -1,3 +1,153 @@
1
+ var __defProp = Object.defineProperty;
2
+ var __export = (target, all) => {
3
+ for (var name in all)
4
+ __defProp(target, name, {
5
+ get: all[name],
6
+ enumerable: true,
7
+ configurable: true,
8
+ set: (newValue) => all[name] = () => newValue
9
+ });
10
+ };
11
+ var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
12
+
13
+ // src/errors.ts
14
+ var exports_errors = {};
15
+ __export(exports_errors, {
16
+ parseServerError: () => parseServerError,
17
+ isTokenExpiredError: () => isTokenExpiredError,
18
+ isAuthorizationError: () => isAuthorizationError,
19
+ isAuthError: () => isAuthError,
20
+ ToolCallError: () => ToolCallError,
21
+ TokenExpiredError: () => TokenExpiredError,
22
+ IntegrateSDKError: () => IntegrateSDKError,
23
+ ConnectionError: () => ConnectionError,
24
+ AuthorizationError: () => AuthorizationError,
25
+ AuthenticationError: () => AuthenticationError
26
+ });
27
+ function isAuthError(error) {
28
+ return error instanceof AuthenticationError;
29
+ }
30
+ function isTokenExpiredError(error) {
31
+ return error instanceof TokenExpiredError;
32
+ }
33
+ function isAuthorizationError(error) {
34
+ return error instanceof AuthorizationError;
35
+ }
36
+ function parseServerError(error, context) {
37
+ if (error && typeof error === "object" && "jsonrpcError" in error) {
38
+ const jsonrpcError = error.jsonrpcError;
39
+ if (jsonrpcError && typeof jsonrpcError === "object") {
40
+ return parseServerError(jsonrpcError, context);
41
+ }
42
+ }
43
+ if (error && typeof error === "object" && "code" in error && "message" in error) {
44
+ const code = error.code;
45
+ const message = error.message || "Unknown error";
46
+ if (code === -32600) {
47
+ return new IntegrateSDKError(`Invalid request: ${message}`);
48
+ }
49
+ if (code === -32601) {
50
+ return new IntegrateSDKError(`Method not found: ${message}`);
51
+ }
52
+ if (code === -32602) {
53
+ return new IntegrateSDKError(`Invalid params: ${message}`);
54
+ }
55
+ if (code === 401 || code === -32001) {
56
+ if (message.toLowerCase().includes("expired") || message.toLowerCase().includes("token")) {
57
+ return new TokenExpiredError(message, context?.provider);
58
+ }
59
+ return new AuthenticationError(message, 401, context?.provider);
60
+ }
61
+ if (code === 403 || code === -32002) {
62
+ return new AuthorizationError(message, 403);
63
+ }
64
+ if (context?.toolName) {
65
+ return new ToolCallError(message, context.toolName, error);
66
+ }
67
+ return new IntegrateSDKError(message);
68
+ }
69
+ if (error instanceof Error) {
70
+ const message = error.message;
71
+ const statusCode = error.statusCode;
72
+ if (statusCode === 401) {
73
+ if (message.toLowerCase().includes("expired") || message.toLowerCase().includes("token")) {
74
+ return new TokenExpiredError(message, context?.provider);
75
+ }
76
+ return new AuthenticationError(message, 401, context?.provider);
77
+ }
78
+ if (statusCode === 403) {
79
+ return new AuthorizationError(message, 403);
80
+ }
81
+ if (message.includes("401") || message.includes("Unauthorized") || message.includes("unauthenticated")) {
82
+ if (message.toLowerCase().includes("expired") || message.toLowerCase().includes("token")) {
83
+ return new TokenExpiredError(message, context?.provider);
84
+ }
85
+ return new AuthenticationError(message, 401, context?.provider);
86
+ }
87
+ if (message.includes("403") || message.includes("Forbidden") || message.includes("unauthorized")) {
88
+ return new AuthorizationError(message, 403);
89
+ }
90
+ if (context?.toolName) {
91
+ return new ToolCallError(message, context.toolName, error);
92
+ }
93
+ return new IntegrateSDKError(message);
94
+ }
95
+ return new IntegrateSDKError(String(error));
96
+ }
97
+ var IntegrateSDKError, AuthenticationError, AuthorizationError, TokenExpiredError, ConnectionError, ToolCallError;
98
+ var init_errors = __esm(() => {
99
+ IntegrateSDKError = class IntegrateSDKError extends Error {
100
+ constructor(message) {
101
+ super(message);
102
+ this.name = "IntegrateSDKError";
103
+ }
104
+ };
105
+ AuthenticationError = class AuthenticationError extends IntegrateSDKError {
106
+ statusCode;
107
+ provider;
108
+ constructor(message, statusCode, provider) {
109
+ super(message);
110
+ this.name = "AuthenticationError";
111
+ this.statusCode = statusCode;
112
+ this.provider = provider;
113
+ }
114
+ };
115
+ AuthorizationError = class AuthorizationError extends IntegrateSDKError {
116
+ statusCode;
117
+ requiredScopes;
118
+ constructor(message, statusCode, requiredScopes) {
119
+ super(message);
120
+ this.name = "AuthorizationError";
121
+ this.statusCode = statusCode;
122
+ this.requiredScopes = requiredScopes;
123
+ }
124
+ };
125
+ TokenExpiredError = class TokenExpiredError extends AuthenticationError {
126
+ constructor(message, provider) {
127
+ super(message, 401, provider);
128
+ this.name = "TokenExpiredError";
129
+ }
130
+ };
131
+ ConnectionError = class ConnectionError extends IntegrateSDKError {
132
+ statusCode;
133
+ constructor(message, statusCode) {
134
+ super(message);
135
+ this.name = "ConnectionError";
136
+ this.statusCode = statusCode;
137
+ }
138
+ };
139
+ ToolCallError = class ToolCallError extends IntegrateSDKError {
140
+ toolName;
141
+ originalError;
142
+ constructor(message, toolName, originalError) {
143
+ super(message);
144
+ this.name = "ToolCallError";
145
+ this.toolName = toolName;
146
+ this.originalError = originalError;
147
+ }
148
+ };
149
+ });
150
+
1
151
  // src/protocol/jsonrpc.ts
2
152
  function parseMessage(message) {
3
153
  try {
@@ -55,7 +205,9 @@ class HttpSessionTransport {
55
205
  });
56
206
  clearTimeout(timeoutId);
57
207
  if (!response.ok) {
58
- throw new Error(`Request failed: ${response.statusText}`);
208
+ const error = new Error(`Request failed: ${response.statusText}`);
209
+ error.statusCode = response.status;
210
+ throw error;
59
211
  }
60
212
  if (!this.sessionId) {
61
213
  const sid = response.headers.get("mcp-session-id");
@@ -67,11 +219,25 @@ class HttpSessionTransport {
67
219
  }
68
220
  const jsonResponse = await response.json();
69
221
  if ("error" in jsonResponse) {
70
- throw new Error(`JSON-RPC Error ${jsonResponse.error.code}: ${jsonResponse.error.message}`);
222
+ const error = new Error(`JSON-RPC Error ${jsonResponse.error.code}: ${jsonResponse.error.message}`);
223
+ error.code = jsonResponse.error.code;
224
+ if (jsonResponse.error.data) {
225
+ error.data = jsonResponse.error.data;
226
+ }
227
+ error.jsonrpcError = jsonResponse.error;
228
+ throw error;
71
229
  }
72
230
  return jsonResponse.result;
73
231
  } catch (error) {
74
- throw error;
232
+ if (error instanceof Error) {
233
+ if (error.name === "AbortError") {
234
+ const timeoutError = new Error("Request timeout");
235
+ timeoutError.code = -32000;
236
+ throw timeoutError;
237
+ }
238
+ throw error;
239
+ }
240
+ throw new Error(String(error));
75
241
  }
76
242
  }
77
243
  async startSSEListener() {
@@ -184,6 +350,7 @@ var MCPMethod;
184
350
  })(MCPMethod ||= {});
185
351
 
186
352
  // src/client.ts
353
+ init_errors();
187
354
  var MCP_SERVER_URL = "https://mcp.integrate.dev/api/v1/mcp";
188
355
 
189
356
  class MCPClient {
@@ -193,6 +360,9 @@ class MCPClient {
193
360
  enabledToolNames = new Set;
194
361
  initialized = false;
195
362
  clientInfo;
363
+ onReauthRequired;
364
+ maxReauthRetries;
365
+ authState = new Map;
196
366
  constructor(config) {
197
367
  this.transport = new HttpSessionTransport({
198
368
  url: MCP_SERVER_URL,
@@ -204,10 +374,15 @@ class MCPClient {
204
374
  name: "integrate-sdk",
205
375
  version: "0.1.0"
206
376
  };
377
+ this.onReauthRequired = config.onReauthRequired;
378
+ this.maxReauthRetries = config.maxReauthRetries ?? 1;
207
379
  for (const plugin of this.plugins) {
208
380
  for (const toolName of plugin.tools) {
209
381
  this.enabledToolNames.add(toolName);
210
382
  }
383
+ if (plugin.oauth) {
384
+ this.authState.set(plugin.oauth.provider, { authenticated: true });
385
+ }
211
386
  }
212
387
  this.initializePlugins();
213
388
  }
@@ -254,6 +429,9 @@ class MCPClient {
254
429
  console.log(`Discovered ${response.tools.length} tools, ${enabledTools.length} enabled by plugins`);
255
430
  }
256
431
  async callTool(name, args) {
432
+ return await this.callToolWithRetry(name, args, 0);
433
+ }
434
+ async callToolWithRetry(name, args, retryCount = 0) {
257
435
  if (!this.initialized) {
258
436
  throw new Error("Client not initialized. Call connect() first.");
259
437
  }
@@ -267,7 +445,44 @@ class MCPClient {
267
445
  name,
268
446
  arguments: args
269
447
  };
270
- return await this.transport.sendRequest("tools/call" /* TOOLS_CALL */, params);
448
+ try {
449
+ const response = await this.transport.sendRequest("tools/call" /* TOOLS_CALL */, params);
450
+ const provider = this.getProviderForTool(name);
451
+ if (provider) {
452
+ this.authState.set(provider, { authenticated: true });
453
+ }
454
+ return response;
455
+ } catch (error) {
456
+ const provider = this.getProviderForTool(name);
457
+ const parsedError = parseServerError(error, { toolName: name, provider });
458
+ if (isAuthError(parsedError) && retryCount < this.maxReauthRetries) {
459
+ if (provider) {
460
+ this.authState.set(provider, {
461
+ authenticated: false,
462
+ lastError: parsedError
463
+ });
464
+ }
465
+ if (this.onReauthRequired && provider) {
466
+ const reauthSuccess = await this.onReauthRequired({
467
+ provider,
468
+ error: parsedError,
469
+ toolName: name
470
+ });
471
+ if (reauthSuccess) {
472
+ return await this.callToolWithRetry(name, args, retryCount + 1);
473
+ }
474
+ }
475
+ }
476
+ throw parsedError;
477
+ }
478
+ }
479
+ getProviderForTool(toolName) {
480
+ for (const plugin of this.plugins) {
481
+ if (plugin.tools.includes(toolName) && plugin.oauth) {
482
+ return plugin.oauth.provider;
483
+ }
484
+ }
485
+ return;
271
486
  }
272
487
  getTool(name) {
273
488
  return this.availableTools.get(name);
@@ -309,10 +524,38 @@ class MCPClient {
309
524
  isInitialized() {
310
525
  return this.initialized;
311
526
  }
527
+ getAuthState(provider) {
528
+ return this.authState.get(provider);
529
+ }
530
+ isProviderAuthenticated(provider) {
531
+ return this.authState.get(provider)?.authenticated ?? false;
532
+ }
533
+ async reauthenticate(provider) {
534
+ const state = this.authState.get(provider);
535
+ if (!state) {
536
+ throw new Error(`Provider "${provider}" not found in configured plugins`);
537
+ }
538
+ if (!this.onReauthRequired) {
539
+ throw new Error("No re-authentication handler configured. Set onReauthRequired in client config.");
540
+ }
541
+ const lastError = state.lastError || new (await Promise.resolve().then(() => (init_errors(), exports_errors))).AuthenticationError("Manual re-authentication requested", undefined, provider);
542
+ const success = await this.onReauthRequired({
543
+ provider,
544
+ error: lastError
545
+ });
546
+ if (success) {
547
+ this.authState.set(provider, { authenticated: true });
548
+ }
549
+ return success;
550
+ }
312
551
  }
313
552
  function createMCPClient(config) {
314
553
  return new MCPClient(config);
315
554
  }
555
+
556
+ // src/index.ts
557
+ init_errors();
558
+
316
559
  // src/plugins/github.ts
317
560
  var GITHUB_TOOLS = [
318
561
  "github_create_issue",
@@ -457,6 +700,10 @@ function getVercelAITools(client) {
457
700
  return convertMCPToolsToVercelAI(client);
458
701
  }
459
702
  export {
703
+ parseServerError,
704
+ isTokenExpiredError,
705
+ isAuthorizationError,
706
+ isAuthError,
460
707
  gmailPlugin,
461
708
  githubPlugin,
462
709
  getVercelAITools,
@@ -465,7 +712,13 @@ export {
465
712
  createMCPClient,
466
713
  convertMCPToolsToVercelAI,
467
714
  convertMCPToolToVercelAI,
715
+ ToolCallError,
716
+ TokenExpiredError,
468
717
  MCPMethod,
469
718
  MCPClient,
470
- HttpSessionTransport
719
+ IntegrateSDKError,
720
+ HttpSessionTransport,
721
+ ConnectionError,
722
+ AuthorizationError,
723
+ AuthenticationError
471
724
  };
@@ -42,7 +42,7 @@ export declare function convertMCPToolToVercelAI(mcpTool: MCPTool, client: MCPCl
42
42
  * const tools = convertMCPToolsToVercelAI(mcpClient);
43
43
  *
44
44
  * const result = await generateText({
45
- * model: openai('gpt-4'),
45
+ * model: openai('gpt-5'),
46
46
  * prompt: 'Create a GitHub issue in my repo',
47
47
  * tools,
48
48
  * });
@@ -1 +1 @@
1
- {"version":3,"file":"http-session.d.ts","sourceRoot":"","sources":["../../src/transport/http-session.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH,OAAO,KAAK,EAEV,eAAe,EACf,mBAAmB,EACpB,MAAM,yBAAyB,CAAC;AAGjC,MAAM,MAAM,cAAc,GAAG,CAC3B,OAAO,EAAE,eAAe,GAAG,mBAAmB,KAC3C,IAAI,CAAC;AAEV,MAAM,WAAW,2BAA2B;IAC1C,GAAG,EAAE,MAAM,CAAC;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IACjC,2CAA2C;IAC3C,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB;AAED;;;;;;GAMG;AACH,qBAAa,oBAAoB;IAC/B,OAAO,CAAC,GAAG,CAAS;IACpB,OAAO,CAAC,OAAO,CAAyB;IACxC,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AAO,CAAC,OAAO,CAAS;IACxB,OAAO,CAAC,eAAe,CAAkC;IACzD,OAAO,CAAC,SAAS,CAAC,CAAS;IAC3B,OAAO,CAAC,aAAa,CAAC,CAAkB;IACxC,OAAO,CAAC,SAAS,CAAS;gBAEd,OAAO,EAAE,2BAA2B;IAMhD;;OAEG;IACG,OAAO,IAAI,OAAO,CAAC,IAAI,CAAC;IAS9B;;OAEG;IACG,WAAW,CAAC,CAAC,GAAG,OAAO,EAC3B,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,CAAC,CAAC;IAsFb;;OAEG;YACW,gBAAgB;IAiC9B;;OAEG;YACW,gBAAgB;IAiC9B;;OAEG;IACH,OAAO,CAAC,kBAAkB;IAiB1B;;OAEG;IACH,SAAS,CAAC,OAAO,EAAE,cAAc,GAAG,MAAM,IAAI;IAS9C;;OAEG;IACG,UAAU,IAAI,OAAO,CAAC,IAAI,CAAC;IAkBjC;;OAEG;IACH,WAAW,IAAI,OAAO;IAItB;;OAEG;IACH,YAAY,IAAI,MAAM,GAAG,SAAS;CAGnC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "integrate-sdk",
3
- "version": "0.1.4",
3
+ "version": "0.1.7",
4
4
  "description": "Type-safe TypeScript SDK for MCP Client with plugin-based OAuth provider configuration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -51,5 +51,4 @@
51
51
  "peerDependencies": {
52
52
  "typescript": ">=5.0.0"
53
53
  }
54
- }
55
-
54
+ }