mcp-use 0.2.0 → 1.0.0
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/chunk-2HFIPY7C.js +429 -0
- package/dist/chunk-4DEFXVWT.js +680 -0
- package/dist/chunk-JXLQRAW2.js +532 -0
- package/dist/chunk-SHUYVCID.js +6 -0
- package/dist/chunk-YUSC6R6V.js +299 -0
- package/dist/index.cjs +5762 -0
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3767 -22
- package/dist/langfuse-YA2S23SM.js +13 -0
- package/dist/src/agents/remote.d.ts.map +1 -1
- package/dist/src/agents/utils/ai_sdk.d.ts.map +1 -1
- package/dist/src/auth/browser-provider.d.ts +52 -0
- package/dist/src/auth/browser-provider.d.ts.map +1 -0
- package/dist/src/auth/callback.d.ts +6 -0
- package/dist/src/auth/callback.d.ts.map +1 -0
- package/dist/src/auth/index.d.ts +7 -0
- package/dist/src/auth/index.d.ts.map +1 -0
- package/dist/src/auth/types.d.ts +18 -0
- package/dist/src/auth/types.d.ts.map +1 -0
- package/dist/src/browser.cjs +323 -0
- package/dist/src/browser.d.ts +5 -46
- package/dist/src/browser.d.ts.map +1 -1
- package/dist/src/browser.js +9 -75
- package/dist/src/oauth-helper.d.ts +2 -12
- package/dist/src/oauth-helper.d.ts.map +1 -1
- package/dist/src/react/index.cjs +986 -0
- package/dist/src/react/index.d.ts +9 -0
- package/dist/src/react/index.d.ts.map +1 -0
- package/dist/src/react/index.js +11 -0
- package/dist/src/react/types.d.ts +139 -0
- package/dist/src/react/types.d.ts.map +1 -0
- package/dist/src/react/useMcp.d.ts +3 -0
- package/dist/src/react/useMcp.d.ts.map +1 -0
- package/dist/src/server/index.cjs +566 -0
- package/dist/src/server/index.d.ts +3 -0
- package/dist/src/server/index.d.ts.map +1 -0
- package/dist/src/server/index.js +9 -0
- package/dist/src/server/logging.d.ts +16 -0
- package/dist/src/server/logging.d.ts.map +1 -0
- package/dist/src/server/mcp-server.d.ts +282 -0
- package/dist/src/server/mcp-server.d.ts.map +1 -0
- package/dist/src/server/types.d.ts +47 -0
- package/dist/src/server/types.d.ts.map +1 -0
- package/dist/src/utils/assert.d.ts +8 -0
- package/dist/src/utils/assert.d.ts.map +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +67 -40
- package/dist/src/adapters/base.js +0 -124
- package/dist/src/adapters/index.js +0 -2
- package/dist/src/adapters/langchain_adapter.js +0 -49
- package/dist/src/agents/base.js +0 -9
- package/dist/src/agents/index.js +0 -3
- package/dist/src/agents/mcp_agent.js +0 -1002
- package/dist/src/agents/prompts/system_prompt_builder.js +0 -40
- package/dist/src/agents/prompts/templates.js +0 -39
- package/dist/src/agents/remote.js +0 -264
- package/dist/src/agents/utils/ai_sdk.js +0 -62
- package/dist/src/agents/utils/index.js +0 -1
- package/dist/src/client/base.js +0 -119
- package/dist/src/client.js +0 -50
- package/dist/src/config.js +0 -34
- package/dist/src/connectors/base.js +0 -143
- package/dist/src/connectors/http.js +0 -150
- package/dist/src/connectors/index.js +0 -4
- package/dist/src/connectors/stdio.js +0 -68
- package/dist/src/connectors/websocket.js +0 -157
- package/dist/src/logging.js +0 -232
- package/dist/src/managers/index.js +0 -2
- package/dist/src/managers/server_manager.js +0 -106
- package/dist/src/managers/tools/acquire_active_mcp_server.js +0 -17
- package/dist/src/managers/tools/add_server_from_config.js +0 -40
- package/dist/src/managers/tools/base.js +0 -17
- package/dist/src/managers/tools/connect_mcp_server.js +0 -46
- package/dist/src/managers/tools/index.js +0 -5
- package/dist/src/managers/tools/list_mcp_servers.js +0 -33
- package/dist/src/managers/tools/release_mcp_server_connection.js +0 -19
- package/dist/src/oauth-helper.js +0 -427
- package/dist/src/observability/index.js +0 -12
- package/dist/src/observability/langfuse.js +0 -211
- package/dist/src/observability/manager.js +0 -199
- package/dist/src/observability/types.js +0 -4
- package/dist/src/session.js +0 -23
- package/dist/src/task_managers/base.js +0 -127
- package/dist/src/task_managers/index.js +0 -5
- package/dist/src/task_managers/sse.js +0 -43
- package/dist/src/task_managers/stdio.js +0 -51
- package/dist/src/task_managers/streamable_http.js +0 -50
- package/dist/src/task_managers/websocket.js +0 -67
- package/dist/src/telemetry/events.js +0 -44
- package/dist/src/telemetry/index.js +0 -8
- package/dist/src/telemetry/telemetry.js +0 -324
- package/dist/src/telemetry/utils.js +0 -39
- package/dist/tests/ai_sdk_compatibility.test.js +0 -214
- package/dist/tests/stream_events.test.js +0 -307
- package/dist/tests/stream_events_simple.test.js +0 -179
- package/dist/vitest.config.js +0 -21
@@ -0,0 +1,299 @@
|
|
1
|
+
import {
|
2
|
+
__name
|
3
|
+
} from "./chunk-SHUYVCID.js";
|
4
|
+
|
5
|
+
// src/auth/browser-provider.ts
|
6
|
+
import { sanitizeUrl } from "strict-url-sanitise";
|
7
|
+
var BrowserOAuthClientProvider = class {
|
8
|
+
static {
|
9
|
+
__name(this, "BrowserOAuthClientProvider");
|
10
|
+
}
|
11
|
+
serverUrl;
|
12
|
+
storageKeyPrefix;
|
13
|
+
serverUrlHash;
|
14
|
+
clientName;
|
15
|
+
clientUri;
|
16
|
+
callbackUrl;
|
17
|
+
preventAutoAuth;
|
18
|
+
onPopupWindow;
|
19
|
+
constructor(serverUrl, options = {}) {
|
20
|
+
this.serverUrl = serverUrl;
|
21
|
+
this.storageKeyPrefix = options.storageKeyPrefix || "mcp:auth";
|
22
|
+
this.serverUrlHash = this.hashString(serverUrl);
|
23
|
+
this.clientName = options.clientName || "MCP Browser Client";
|
24
|
+
this.clientUri = options.clientUri || (typeof window !== "undefined" ? window.location.origin : "");
|
25
|
+
this.callbackUrl = sanitizeUrl(
|
26
|
+
options.callbackUrl || (typeof window !== "undefined" ? new URL("/oauth/callback", window.location.origin).toString() : "/oauth/callback")
|
27
|
+
);
|
28
|
+
this.preventAutoAuth = options.preventAutoAuth;
|
29
|
+
this.onPopupWindow = options.onPopupWindow;
|
30
|
+
}
|
31
|
+
// --- SDK Interface Methods ---
|
32
|
+
get redirectUrl() {
|
33
|
+
return sanitizeUrl(this.callbackUrl);
|
34
|
+
}
|
35
|
+
get clientMetadata() {
|
36
|
+
return {
|
37
|
+
redirect_uris: [this.redirectUrl],
|
38
|
+
token_endpoint_auth_method: "none",
|
39
|
+
// Public client
|
40
|
+
grant_types: ["authorization_code", "refresh_token"],
|
41
|
+
response_types: ["code"],
|
42
|
+
client_name: this.clientName,
|
43
|
+
client_uri: this.clientUri
|
44
|
+
// scope: 'openid profile email mcp', // Example scopes, adjust as needed
|
45
|
+
};
|
46
|
+
}
|
47
|
+
async clientInformation() {
|
48
|
+
const key = this.getKey("client_info");
|
49
|
+
const data = localStorage.getItem(key);
|
50
|
+
if (!data) return void 0;
|
51
|
+
try {
|
52
|
+
return JSON.parse(data);
|
53
|
+
} catch (e) {
|
54
|
+
console.warn(`[${this.storageKeyPrefix}] Failed to parse client information:`, e);
|
55
|
+
localStorage.removeItem(key);
|
56
|
+
return void 0;
|
57
|
+
}
|
58
|
+
}
|
59
|
+
// NOTE: The SDK's auth() function uses this if dynamic registration is needed.
|
60
|
+
// Ensure your OAuthClientInformationFull matches the expected structure if DCR is used.
|
61
|
+
async saveClientInformation(clientInformation) {
|
62
|
+
const key = this.getKey("client_info");
|
63
|
+
localStorage.setItem(key, JSON.stringify(clientInformation));
|
64
|
+
}
|
65
|
+
async tokens() {
|
66
|
+
const key = this.getKey("tokens");
|
67
|
+
const data = localStorage.getItem(key);
|
68
|
+
if (!data) return void 0;
|
69
|
+
try {
|
70
|
+
return JSON.parse(data);
|
71
|
+
} catch (e) {
|
72
|
+
console.warn(`[${this.storageKeyPrefix}] Failed to parse tokens:`, e);
|
73
|
+
localStorage.removeItem(key);
|
74
|
+
return void 0;
|
75
|
+
}
|
76
|
+
}
|
77
|
+
async saveTokens(tokens) {
|
78
|
+
const key = this.getKey("tokens");
|
79
|
+
localStorage.setItem(key, JSON.stringify(tokens));
|
80
|
+
localStorage.removeItem(this.getKey("code_verifier"));
|
81
|
+
localStorage.removeItem(this.getKey("last_auth_url"));
|
82
|
+
}
|
83
|
+
async saveCodeVerifier(codeVerifier) {
|
84
|
+
const key = this.getKey("code_verifier");
|
85
|
+
localStorage.setItem(key, codeVerifier);
|
86
|
+
}
|
87
|
+
async codeVerifier() {
|
88
|
+
const key = this.getKey("code_verifier");
|
89
|
+
const verifier = localStorage.getItem(key);
|
90
|
+
if (!verifier) {
|
91
|
+
throw new Error(
|
92
|
+
`[${this.storageKeyPrefix}] Code verifier not found in storage for key ${key}. Auth flow likely corrupted or timed out.`
|
93
|
+
);
|
94
|
+
}
|
95
|
+
return verifier;
|
96
|
+
}
|
97
|
+
/**
|
98
|
+
* Generates and stores the authorization URL with state, without opening a popup.
|
99
|
+
* Used when preventAutoAuth is enabled to provide the URL for manual navigation.
|
100
|
+
* @param authorizationUrl The fully constructed authorization URL from the SDK.
|
101
|
+
* @returns The full authorization URL with state parameter.
|
102
|
+
*/
|
103
|
+
async prepareAuthorizationUrl(authorizationUrl) {
|
104
|
+
const state = globalThis.crypto.randomUUID();
|
105
|
+
const stateKey = `${this.storageKeyPrefix}:state_${state}`;
|
106
|
+
const stateData = {
|
107
|
+
serverUrlHash: this.serverUrlHash,
|
108
|
+
expiry: Date.now() + 1e3 * 60 * 10,
|
109
|
+
// State expires in 10 minutes
|
110
|
+
// Store provider options needed to reconstruct on callback
|
111
|
+
providerOptions: {
|
112
|
+
serverUrl: this.serverUrl,
|
113
|
+
storageKeyPrefix: this.storageKeyPrefix,
|
114
|
+
clientName: this.clientName,
|
115
|
+
clientUri: this.clientUri,
|
116
|
+
callbackUrl: this.callbackUrl
|
117
|
+
}
|
118
|
+
};
|
119
|
+
localStorage.setItem(stateKey, JSON.stringify(stateData));
|
120
|
+
authorizationUrl.searchParams.set("state", state);
|
121
|
+
const authUrlString = authorizationUrl.toString();
|
122
|
+
const sanitizedAuthUrl = sanitizeUrl(authUrlString);
|
123
|
+
localStorage.setItem(this.getKey("last_auth_url"), sanitizedAuthUrl);
|
124
|
+
return sanitizedAuthUrl;
|
125
|
+
}
|
126
|
+
/**
|
127
|
+
* Redirects the user agent to the authorization URL, storing necessary state.
|
128
|
+
* This now adheres to the SDK's void return type expectation for the interface.
|
129
|
+
* @param authorizationUrl The fully constructed authorization URL from the SDK.
|
130
|
+
*/
|
131
|
+
async redirectToAuthorization(authorizationUrl) {
|
132
|
+
if (this.preventAutoAuth) return;
|
133
|
+
const sanitizedAuthUrl = await this.prepareAuthorizationUrl(authorizationUrl);
|
134
|
+
const popupFeatures = "width=600,height=700,resizable=yes,scrollbars=yes,status=yes";
|
135
|
+
try {
|
136
|
+
const popup = window.open(sanitizedAuthUrl, `mcp_auth_${this.serverUrlHash}`, popupFeatures);
|
137
|
+
if (this.onPopupWindow) {
|
138
|
+
this.onPopupWindow(sanitizedAuthUrl, popupFeatures, popup);
|
139
|
+
}
|
140
|
+
if (!popup || popup.closed || typeof popup.closed === "undefined") {
|
141
|
+
console.warn(
|
142
|
+
`[${this.storageKeyPrefix}] Popup likely blocked by browser. Manual navigation might be required using the stored URL.`
|
143
|
+
);
|
144
|
+
} else {
|
145
|
+
popup.focus();
|
146
|
+
console.info(`[${this.storageKeyPrefix}] Redirecting to authorization URL in popup.`);
|
147
|
+
}
|
148
|
+
} catch (e) {
|
149
|
+
console.error(`[${this.storageKeyPrefix}] Error opening popup window:`, e);
|
150
|
+
}
|
151
|
+
}
|
152
|
+
// --- Helper Methods ---
|
153
|
+
/**
|
154
|
+
* Retrieves the last URL passed to `redirectToAuthorization`. Useful for manual fallback.
|
155
|
+
*/
|
156
|
+
getLastAttemptedAuthUrl() {
|
157
|
+
const storedUrl = localStorage.getItem(this.getKey("last_auth_url"));
|
158
|
+
return storedUrl ? sanitizeUrl(storedUrl) : null;
|
159
|
+
}
|
160
|
+
clearStorage() {
|
161
|
+
const prefixPattern = `${this.storageKeyPrefix}_${this.serverUrlHash}_`;
|
162
|
+
const statePattern = `${this.storageKeyPrefix}:state_`;
|
163
|
+
const keysToRemove = [];
|
164
|
+
let count = 0;
|
165
|
+
for (let i = 0; i < localStorage.length; i++) {
|
166
|
+
const key = localStorage.key(i);
|
167
|
+
if (!key) continue;
|
168
|
+
if (key.startsWith(prefixPattern)) {
|
169
|
+
keysToRemove.push(key);
|
170
|
+
} else if (key.startsWith(statePattern)) {
|
171
|
+
try {
|
172
|
+
const item = localStorage.getItem(key);
|
173
|
+
if (item) {
|
174
|
+
const state = JSON.parse(item);
|
175
|
+
if (state.serverUrlHash === this.serverUrlHash) {
|
176
|
+
keysToRemove.push(key);
|
177
|
+
}
|
178
|
+
}
|
179
|
+
} catch (e) {
|
180
|
+
console.warn(`[${this.storageKeyPrefix}] Error parsing state key ${key} during clearStorage:`, e);
|
181
|
+
}
|
182
|
+
}
|
183
|
+
}
|
184
|
+
const uniqueKeysToRemove = [...new Set(keysToRemove)];
|
185
|
+
uniqueKeysToRemove.forEach((key) => {
|
186
|
+
localStorage.removeItem(key);
|
187
|
+
count++;
|
188
|
+
});
|
189
|
+
return count;
|
190
|
+
}
|
191
|
+
hashString(str) {
|
192
|
+
let hash = 0;
|
193
|
+
for (let i = 0; i < str.length; i++) {
|
194
|
+
const char = str.charCodeAt(i);
|
195
|
+
hash = (hash << 5) - hash + char;
|
196
|
+
hash = hash & hash;
|
197
|
+
}
|
198
|
+
return Math.abs(hash).toString(16);
|
199
|
+
}
|
200
|
+
getKey(keySuffix) {
|
201
|
+
return `${this.storageKeyPrefix}_${this.serverUrlHash}_${keySuffix}`;
|
202
|
+
}
|
203
|
+
};
|
204
|
+
|
205
|
+
// src/auth/callback.ts
|
206
|
+
import { auth } from "@modelcontextprotocol/sdk/client/auth.js";
|
207
|
+
async function onMcpAuthorization() {
|
208
|
+
const queryParams = new URLSearchParams(window.location.search);
|
209
|
+
const code = queryParams.get("code");
|
210
|
+
const state = queryParams.get("state");
|
211
|
+
const error = queryParams.get("error");
|
212
|
+
const errorDescription = queryParams.get("error_description");
|
213
|
+
const logPrefix = "[mcp-callback]";
|
214
|
+
console.log(`${logPrefix} Handling callback...`, { code, state, error, errorDescription });
|
215
|
+
let provider = null;
|
216
|
+
let storedStateData = null;
|
217
|
+
const stateKey = state ? `mcp:auth:state_${state}` : null;
|
218
|
+
try {
|
219
|
+
if (error) {
|
220
|
+
throw new Error(`OAuth error: ${error} - ${errorDescription || "No description provided."}`);
|
221
|
+
}
|
222
|
+
if (!code) {
|
223
|
+
throw new Error("Authorization code not found in callback query parameters.");
|
224
|
+
}
|
225
|
+
if (!state || !stateKey) {
|
226
|
+
throw new Error("State parameter not found or invalid in callback query parameters.");
|
227
|
+
}
|
228
|
+
const storedStateJSON = localStorage.getItem(stateKey);
|
229
|
+
if (!storedStateJSON) {
|
230
|
+
throw new Error(`Invalid or expired state parameter "${state}". No matching state found in storage.`);
|
231
|
+
}
|
232
|
+
try {
|
233
|
+
storedStateData = JSON.parse(storedStateJSON);
|
234
|
+
} catch (e) {
|
235
|
+
throw new Error("Failed to parse stored OAuth state.");
|
236
|
+
}
|
237
|
+
if (!storedStateData.expiry || storedStateData.expiry < Date.now()) {
|
238
|
+
localStorage.removeItem(stateKey);
|
239
|
+
throw new Error("OAuth state has expired. Please try initiating authentication again.");
|
240
|
+
}
|
241
|
+
if (!storedStateData.providerOptions) {
|
242
|
+
throw new Error("Stored state is missing required provider options.");
|
243
|
+
}
|
244
|
+
const { serverUrl, ...providerOptions } = storedStateData.providerOptions;
|
245
|
+
console.log(`${logPrefix} Re-instantiating provider for server: ${serverUrl}`);
|
246
|
+
provider = new BrowserOAuthClientProvider(serverUrl, providerOptions);
|
247
|
+
console.log(`${logPrefix} Calling SDK auth() to exchange code...`);
|
248
|
+
const authResult = await auth(provider, { serverUrl, authorizationCode: code });
|
249
|
+
if (authResult === "AUTHORIZED") {
|
250
|
+
console.log(`${logPrefix} Authorization successful via SDK auth(). Notifying opener...`);
|
251
|
+
if (window.opener && !window.opener.closed) {
|
252
|
+
window.opener.postMessage({ type: "mcp_auth_callback", success: true }, window.location.origin);
|
253
|
+
window.close();
|
254
|
+
} else {
|
255
|
+
console.warn(`${logPrefix} No opener window detected. Redirecting to root.`);
|
256
|
+
const pathParts = window.location.pathname.split("/").filter(Boolean);
|
257
|
+
const basePath = pathParts.length > 0 && pathParts[pathParts.length - 1] === "callback" ? "/" + pathParts.slice(0, -2).join("/") : "/";
|
258
|
+
window.location.href = basePath || "/";
|
259
|
+
}
|
260
|
+
localStorage.removeItem(stateKey);
|
261
|
+
} else {
|
262
|
+
console.warn(`${logPrefix} SDK auth() returned unexpected status: ${authResult}`);
|
263
|
+
throw new Error(`Unexpected result from authentication library: ${authResult}`);
|
264
|
+
}
|
265
|
+
} catch (err) {
|
266
|
+
console.error(`${logPrefix} Error during OAuth callback handling:`, err);
|
267
|
+
const errorMessage = err instanceof Error ? err.message : String(err);
|
268
|
+
if (window.opener && !window.opener.closed) {
|
269
|
+
window.opener.postMessage({ type: "mcp_auth_callback", success: false, error: errorMessage }, window.location.origin);
|
270
|
+
}
|
271
|
+
try {
|
272
|
+
document.body.innerHTML = `
|
273
|
+
<div style="font-family: sans-serif; padding: 20px;">
|
274
|
+
<h1>Authentication Error</h1>
|
275
|
+
<p style="color: red; background-color: #ffebeb; border: 1px solid red; padding: 10px; border-radius: 4px;">
|
276
|
+
${errorMessage}
|
277
|
+
</p>
|
278
|
+
<p>You can close this window or <a href="#" onclick="window.close(); return false;">click here to close</a>.</p>
|
279
|
+
<pre style="font-size: 0.8em; color: #555; margin-top: 20px; white-space: pre-wrap;">${err instanceof Error ? err.stack : ""}</pre>
|
280
|
+
</div>
|
281
|
+
`;
|
282
|
+
} catch (displayError) {
|
283
|
+
console.error(`${logPrefix} Could not display error in callback window:`, displayError);
|
284
|
+
}
|
285
|
+
if (stateKey) {
|
286
|
+
localStorage.removeItem(stateKey);
|
287
|
+
}
|
288
|
+
if (provider) {
|
289
|
+
localStorage.removeItem(provider.getKey("code_verifier"));
|
290
|
+
localStorage.removeItem(provider.getKey("last_auth_url"));
|
291
|
+
}
|
292
|
+
}
|
293
|
+
}
|
294
|
+
__name(onMcpAuthorization, "onMcpAuthorization");
|
295
|
+
|
296
|
+
export {
|
297
|
+
BrowserOAuthClientProvider,
|
298
|
+
onMcpAuthorization
|
299
|
+
};
|