mcp-use 0.1.20 → 0.3.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 +8 -0
- package/dist/src/browser.d.ts.map +1 -0
- package/dist/src/browser.js +9 -0
- package/dist/src/client/base.d.ts +32 -0
- package/dist/src/client/base.d.ts.map +1 -0
- package/dist/src/client.d.ts +19 -16
- package/dist/src/client.d.ts.map +1 -1
- package/dist/src/logging.d.ts +1 -1
- package/dist/src/logging.d.ts.map +1 -1
- package/dist/src/oauth-helper.d.ts +125 -0
- package/dist/src/oauth-helper.d.ts.map +1 -0
- 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 +72 -40
- package/dist/examples/add_server_tool.d.ts +0 -8
- package/dist/examples/add_server_tool.d.ts.map +0 -1
- package/dist/examples/add_server_tool.js +0 -79
- package/dist/examples/ai_sdk_example.d.ts +0 -23
- package/dist/examples/ai_sdk_example.d.ts.map +0 -1
- package/dist/examples/ai_sdk_example.js +0 -213
- package/dist/examples/airbnb_use.d.ts +0 -10
- package/dist/examples/airbnb_use.d.ts.map +0 -1
- package/dist/examples/airbnb_use.js +0 -43
- package/dist/examples/blender_use.d.ts +0 -15
- package/dist/examples/blender_use.d.ts.map +0 -1
- package/dist/examples/blender_use.js +0 -39
- package/dist/examples/browser_use.d.ts +0 -10
- package/dist/examples/browser_use.d.ts.map +0 -1
- package/dist/examples/browser_use.js +0 -46
- package/dist/examples/chat_example.d.ts +0 -10
- package/dist/examples/chat_example.d.ts.map +0 -1
- package/dist/examples/chat_example.js +0 -86
- package/dist/examples/filesystem_use.d.ts +0 -11
- package/dist/examples/filesystem_use.d.ts.map +0 -1
- package/dist/examples/filesystem_use.js +0 -43
- package/dist/examples/http_example.d.ts +0 -18
- package/dist/examples/http_example.d.ts.map +0 -1
- package/dist/examples/http_example.js +0 -37
- package/dist/examples/mcp_everything.d.ts +0 -6
- package/dist/examples/mcp_everything.d.ts.map +0 -1
- package/dist/examples/mcp_everything.js +0 -25
- package/dist/examples/multi_server_example.d.ts +0 -10
- package/dist/examples/multi_server_example.d.ts.map +0 -1
- package/dist/examples/multi_server_example.js +0 -51
- package/dist/examples/observability.d.ts +0 -6
- package/dist/examples/observability.d.ts.map +0 -1
- package/dist/examples/observability.js +0 -50
- package/dist/examples/stream_example.d.ts +0 -12
- package/dist/examples/stream_example.d.ts.map +0 -1
- package/dist/examples/stream_example.js +0 -198
- package/dist/examples/structured_output.d.ts +0 -9
- package/dist/examples/structured_output.d.ts.map +0 -1
- package/dist/examples/structured_output.js +0 -95
- 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.js +0 -133
- 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 -217
- 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/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
@@ -1,211 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Langfuse observability integration for MCP-use.
|
3
|
-
*
|
4
|
-
* This module provides automatic instrumentation and callback handler
|
5
|
-
* for Langfuse observability platform.
|
6
|
-
*/
|
7
|
-
import { config } from 'dotenv';
|
8
|
-
import { logger } from '../logging.js';
|
9
|
-
config();
|
10
|
-
// Check if Langfuse is disabled via environment variable
|
11
|
-
const langfuseDisabled = process.env.MCP_USE_LANGFUSE?.toLowerCase() === 'false';
|
12
|
-
// Initialize variables - using const with object to avoid linter issues with mutable exports
|
13
|
-
const langfuseState = {
|
14
|
-
handler: null,
|
15
|
-
client: null,
|
16
|
-
initPromise: null,
|
17
|
-
};
|
18
|
-
async function initializeLangfuse(agentId, metadata, metadataProvider, tagsProvider) {
|
19
|
-
try {
|
20
|
-
// Dynamically import to avoid errors if package not installed
|
21
|
-
const langfuseModule = await import('langfuse-langchain').catch(() => null);
|
22
|
-
if (!langfuseModule) {
|
23
|
-
logger.debug('Langfuse package not installed - tracing disabled. Install with: npm install langfuse-langchain');
|
24
|
-
return;
|
25
|
-
}
|
26
|
-
const { CallbackHandler } = langfuseModule;
|
27
|
-
// Create a custom CallbackHandler wrapper to add logging and custom metadata
|
28
|
-
class LoggingCallbackHandler extends CallbackHandler {
|
29
|
-
agentId;
|
30
|
-
metadata;
|
31
|
-
metadataProvider;
|
32
|
-
tagsProvider;
|
33
|
-
verbose;
|
34
|
-
constructor(config, agentId, metadata, metadataProvider, tagsProvider) {
|
35
|
-
super(config);
|
36
|
-
this.agentId = agentId;
|
37
|
-
this.metadata = metadata;
|
38
|
-
this.metadataProvider = metadataProvider;
|
39
|
-
this.tagsProvider = tagsProvider;
|
40
|
-
this.verbose = config?.verbose ?? false;
|
41
|
-
}
|
42
|
-
// Override to add custom metadata to traces
|
43
|
-
async handleChainStart(chain, inputs, runId, parentRunId, tags, metadata, name, kwargs) {
|
44
|
-
logger.debug('Langfuse: Chain start intercepted');
|
45
|
-
// Add custom tags and metadata
|
46
|
-
const customTags = this.getCustomTags();
|
47
|
-
const metadataToAdd = this.getMetadata();
|
48
|
-
// Merge with existing tags and metadata
|
49
|
-
const enhancedTags = [...(tags || []), ...customTags];
|
50
|
-
const enhancedMetadata = { ...(metadata || {}), ...metadataToAdd };
|
51
|
-
if (this.verbose) {
|
52
|
-
logger.debug(`Langfuse: Chain start with custom tags: ${JSON.stringify(enhancedTags)}`);
|
53
|
-
logger.debug(`Langfuse: Chain start with metadata: ${JSON.stringify(enhancedMetadata)}`);
|
54
|
-
}
|
55
|
-
return super.handleChainStart(chain, inputs, runId, parentRunId, enhancedTags, enhancedMetadata, name, kwargs);
|
56
|
-
}
|
57
|
-
// Get custom tags based on environment and agent configuration
|
58
|
-
getCustomTags() {
|
59
|
-
const tags = [];
|
60
|
-
// Add environment tag
|
61
|
-
const env = this.getEnvironmentTag();
|
62
|
-
if (env) {
|
63
|
-
tags.push(`env:${env}`);
|
64
|
-
}
|
65
|
-
// Add agent ID tag if available
|
66
|
-
if (this.agentId) {
|
67
|
-
tags.push(`agent_id:${this.agentId}`);
|
68
|
-
}
|
69
|
-
// Add tags from provider if available
|
70
|
-
if (this.tagsProvider) {
|
71
|
-
const providerTags = this.tagsProvider();
|
72
|
-
if (providerTags && providerTags.length > 0) {
|
73
|
-
tags.push(...providerTags);
|
74
|
-
}
|
75
|
-
}
|
76
|
-
return tags;
|
77
|
-
}
|
78
|
-
// Get metadata
|
79
|
-
getMetadata() {
|
80
|
-
const metadata = {};
|
81
|
-
// Add environment metadata
|
82
|
-
const env = this.getEnvironmentTag();
|
83
|
-
if (env) {
|
84
|
-
metadata.env = env;
|
85
|
-
}
|
86
|
-
// Add agent ID metadata if available
|
87
|
-
if (this.agentId) {
|
88
|
-
metadata.agent_id = this.agentId;
|
89
|
-
}
|
90
|
-
// Add static metadata if provided
|
91
|
-
if (this.metadata) {
|
92
|
-
Object.assign(metadata, this.metadata);
|
93
|
-
}
|
94
|
-
// Add dynamic metadata from provider if available
|
95
|
-
if (this.metadataProvider) {
|
96
|
-
const dynamicMetadata = this.metadataProvider();
|
97
|
-
if (dynamicMetadata) {
|
98
|
-
Object.assign(metadata, dynamicMetadata);
|
99
|
-
}
|
100
|
-
}
|
101
|
-
return metadata;
|
102
|
-
}
|
103
|
-
// Determine environment tag based on MCP_USE_AGENT_ENV
|
104
|
-
getEnvironmentTag() {
|
105
|
-
const agentEnv = process.env.MCP_USE_AGENT_ENV;
|
106
|
-
if (!agentEnv) {
|
107
|
-
// Default to 'unknown' if environment is not explicitly set
|
108
|
-
return 'unknown';
|
109
|
-
}
|
110
|
-
const envLower = agentEnv.toLowerCase();
|
111
|
-
if (envLower === 'local' || envLower === 'development') {
|
112
|
-
return 'local';
|
113
|
-
}
|
114
|
-
else if (envLower === 'production' || envLower === 'prod') {
|
115
|
-
return 'production';
|
116
|
-
}
|
117
|
-
else if (envLower === 'staging' || envLower === 'stage') {
|
118
|
-
return 'staging';
|
119
|
-
}
|
120
|
-
else if (envLower === 'hosted' || envLower === 'cloud') {
|
121
|
-
return 'hosted';
|
122
|
-
}
|
123
|
-
// For any other values, use the value as-is but sanitized
|
124
|
-
return envLower.replace(/[^a-z0-9_-]/g, '_');
|
125
|
-
}
|
126
|
-
async handleLLMStart(...args) {
|
127
|
-
logger.debug('Langfuse: LLM start intercepted');
|
128
|
-
if (this.verbose) {
|
129
|
-
logger.debug(`Langfuse: LLM start args: ${JSON.stringify(args)}`);
|
130
|
-
}
|
131
|
-
return super.handleLLMStart(...args);
|
132
|
-
}
|
133
|
-
async handleToolStart(...args) {
|
134
|
-
logger.debug('Langfuse: Tool start intercepted');
|
135
|
-
if (this.verbose) {
|
136
|
-
logger.debug(`Langfuse: Tool start args: ${JSON.stringify(args)}`);
|
137
|
-
}
|
138
|
-
return super.handleToolStart(...args);
|
139
|
-
}
|
140
|
-
async handleRetrieverStart(...args) {
|
141
|
-
logger.debug('Langfuse: Retriever start intercepted');
|
142
|
-
if (this.verbose) {
|
143
|
-
logger.debug(`Langfuse: Retriever start args: ${JSON.stringify(args)}`);
|
144
|
-
}
|
145
|
-
return super.handleRetrieverStart(...args);
|
146
|
-
}
|
147
|
-
async handleAgentAction(...args) {
|
148
|
-
logger.debug('Langfuse: Agent action intercepted');
|
149
|
-
if (this.verbose) {
|
150
|
-
logger.debug(`Langfuse: Agent action args: ${JSON.stringify(args)}`);
|
151
|
-
}
|
152
|
-
return super.handleAgentAction(...args);
|
153
|
-
}
|
154
|
-
async handleAgentEnd(...args) {
|
155
|
-
logger.debug('Langfuse: Agent end intercepted');
|
156
|
-
if (this.verbose) {
|
157
|
-
logger.debug(`Langfuse: Agent end args: ${JSON.stringify(args)}`);
|
158
|
-
}
|
159
|
-
return super.handleAgentEnd(...args);
|
160
|
-
}
|
161
|
-
}
|
162
|
-
// Create the handler with configuration
|
163
|
-
const config = {
|
164
|
-
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
165
|
-
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
166
|
-
baseUrl: process.env.LANGFUSE_HOST || process.env.LANGFUSE_BASEURL || 'https://cloud.langfuse.com',
|
167
|
-
flushAt: Number.parseInt(process.env.LANGFUSE_FLUSH_AT || '15'),
|
168
|
-
flushInterval: Number.parseInt(process.env.LANGFUSE_FLUSH_INTERVAL || '10000'),
|
169
|
-
release: process.env.LANGFUSE_RELEASE,
|
170
|
-
requestTimeout: Number.parseInt(process.env.LANGFUSE_REQUEST_TIMEOUT || '10000'),
|
171
|
-
enabled: process.env.LANGFUSE_ENABLED !== 'false',
|
172
|
-
};
|
173
|
-
langfuseState.handler = new LoggingCallbackHandler(config, agentId, metadata, metadataProvider, tagsProvider);
|
174
|
-
logger.debug('Langfuse observability initialized successfully with logging enabled');
|
175
|
-
// Also initialize the client for direct usage if needed
|
176
|
-
try {
|
177
|
-
const langfuseCore = await import('langfuse').catch(() => null);
|
178
|
-
if (langfuseCore) {
|
179
|
-
const { Langfuse } = langfuseCore;
|
180
|
-
langfuseState.client = new Langfuse({
|
181
|
-
publicKey: process.env.LANGFUSE_PUBLIC_KEY,
|
182
|
-
secretKey: process.env.LANGFUSE_SECRET_KEY,
|
183
|
-
baseUrl: process.env.LANGFUSE_HOST || 'https://cloud.langfuse.com',
|
184
|
-
});
|
185
|
-
logger.debug('Langfuse client initialized');
|
186
|
-
}
|
187
|
-
}
|
188
|
-
catch (error) {
|
189
|
-
logger.debug(`Langfuse client initialization failed: ${error}`);
|
190
|
-
}
|
191
|
-
}
|
192
|
-
catch (error) {
|
193
|
-
logger.debug(`Langfuse initialization error: ${error}`);
|
194
|
-
}
|
195
|
-
}
|
196
|
-
// Only initialize if not disabled and required keys are present
|
197
|
-
if (langfuseDisabled) {
|
198
|
-
logger.debug('Langfuse tracing disabled via MCP_USE_LANGFUSE environment variable');
|
199
|
-
}
|
200
|
-
else if (!process.env.LANGFUSE_PUBLIC_KEY || !process.env.LANGFUSE_SECRET_KEY) {
|
201
|
-
logger.debug('Langfuse API keys not found - tracing disabled. Set LANGFUSE_PUBLIC_KEY and LANGFUSE_SECRET_KEY to enable');
|
202
|
-
}
|
203
|
-
else {
|
204
|
-
// Create initialization promise to ensure handlers are ready when needed
|
205
|
-
langfuseState.initPromise = initializeLangfuse();
|
206
|
-
}
|
207
|
-
// Export getters to access the state
|
208
|
-
export const langfuseHandler = () => langfuseState.handler;
|
209
|
-
export const langfuseClient = () => langfuseState.client;
|
210
|
-
export const langfuseInitPromise = () => langfuseState.initPromise;
|
211
|
-
export { initializeLangfuse };
|
@@ -1,199 +0,0 @@
|
|
1
|
-
/**
|
2
|
-
* Observability callbacks manager for MCP-use.
|
3
|
-
*
|
4
|
-
* This module provides a centralized manager for handling observability callbacks
|
5
|
-
* from various platforms (Langfuse, Laminar, etc.) in a clean and extensible way.
|
6
|
-
*/
|
7
|
-
import { logger } from '../logging.js';
|
8
|
-
export class ObservabilityManager {
|
9
|
-
customCallbacks;
|
10
|
-
availableHandlers = [];
|
11
|
-
handlerNames = [];
|
12
|
-
initialized = false;
|
13
|
-
verbose;
|
14
|
-
observe;
|
15
|
-
agentId;
|
16
|
-
metadata;
|
17
|
-
metadataProvider;
|
18
|
-
tagsProvider;
|
19
|
-
constructor(config = {}) {
|
20
|
-
this.customCallbacks = config.customCallbacks;
|
21
|
-
this.verbose = config.verbose ?? false;
|
22
|
-
this.observe = config.observe ?? true;
|
23
|
-
this.agentId = config.agentId;
|
24
|
-
this.metadata = config.metadata;
|
25
|
-
this.metadataProvider = config.metadataProvider;
|
26
|
-
this.tagsProvider = config.tagsProvider;
|
27
|
-
}
|
28
|
-
/**
|
29
|
-
* Collect all available observability handlers from configured platforms.
|
30
|
-
*/
|
31
|
-
async collectAvailableHandlers() {
|
32
|
-
if (this.initialized) {
|
33
|
-
return;
|
34
|
-
}
|
35
|
-
// Import handlers lazily to avoid circular imports
|
36
|
-
try {
|
37
|
-
const { langfuseHandler, langfuseInitPromise } = await import('./langfuse.js');
|
38
|
-
// If we have an agent ID, metadata, or providers, we need to reinitialize Langfuse
|
39
|
-
if (this.agentId || this.metadata || this.metadataProvider || this.tagsProvider) {
|
40
|
-
// Import the initialization function directly
|
41
|
-
const { initializeLangfuse } = await import('./langfuse.js');
|
42
|
-
await initializeLangfuse(this.agentId, this.metadata, this.metadataProvider, this.tagsProvider);
|
43
|
-
logger.debug(`ObservabilityManager: Reinitialized Langfuse with agent ID: ${this.agentId}, metadata: ${JSON.stringify(this.metadata)}`);
|
44
|
-
}
|
45
|
-
else {
|
46
|
-
// Wait for existing initialization to complete
|
47
|
-
const initPromise = langfuseInitPromise();
|
48
|
-
if (initPromise) {
|
49
|
-
await initPromise;
|
50
|
-
}
|
51
|
-
}
|
52
|
-
const handler = langfuseHandler();
|
53
|
-
if (handler) {
|
54
|
-
this.availableHandlers.push(handler);
|
55
|
-
this.handlerNames.push('Langfuse');
|
56
|
-
logger.debug('ObservabilityManager: Langfuse handler available');
|
57
|
-
}
|
58
|
-
}
|
59
|
-
catch {
|
60
|
-
logger.debug('ObservabilityManager: Langfuse module not available');
|
61
|
-
}
|
62
|
-
// Future: Add more platforms here...
|
63
|
-
this.initialized = true;
|
64
|
-
}
|
65
|
-
/**
|
66
|
-
* Get the list of callbacks to use.
|
67
|
-
* @returns List of callbacks - either custom callbacks if provided, or all available observability handlers.
|
68
|
-
*/
|
69
|
-
async getCallbacks() {
|
70
|
-
// If observability is disabled, return empty array
|
71
|
-
if (!this.observe) {
|
72
|
-
logger.debug('ObservabilityManager: Observability disabled via observe=false');
|
73
|
-
return [];
|
74
|
-
}
|
75
|
-
// If custom callbacks were provided, use those
|
76
|
-
if (this.customCallbacks) {
|
77
|
-
logger.debug(`ObservabilityManager: Using ${this.customCallbacks.length} custom callbacks`);
|
78
|
-
return this.customCallbacks;
|
79
|
-
}
|
80
|
-
// Otherwise, collect and return all available handlers
|
81
|
-
await this.collectAvailableHandlers();
|
82
|
-
if (this.availableHandlers.length > 0) {
|
83
|
-
logger.debug(`ObservabilityManager: Using ${this.availableHandlers.length} handlers`);
|
84
|
-
}
|
85
|
-
else {
|
86
|
-
logger.debug('ObservabilityManager: No callbacks configured');
|
87
|
-
}
|
88
|
-
return this.availableHandlers;
|
89
|
-
}
|
90
|
-
/**
|
91
|
-
* Get the names of available handlers.
|
92
|
-
* @returns List of handler names (e.g., ["Langfuse", "Laminar"])
|
93
|
-
*/
|
94
|
-
async getHandlerNames() {
|
95
|
-
// If observability is disabled, return empty array
|
96
|
-
if (!this.observe) {
|
97
|
-
return [];
|
98
|
-
}
|
99
|
-
if (this.customCallbacks) {
|
100
|
-
// For custom callbacks, try to get their class names
|
101
|
-
return this.customCallbacks.map(cb => cb.constructor.name);
|
102
|
-
}
|
103
|
-
await this.collectAvailableHandlers();
|
104
|
-
return this.handlerNames;
|
105
|
-
}
|
106
|
-
/**
|
107
|
-
* Check if any callbacks are available.
|
108
|
-
* @returns True if callbacks are available, False otherwise.
|
109
|
-
*/
|
110
|
-
async hasCallbacks() {
|
111
|
-
// If observability is disabled, no callbacks are available
|
112
|
-
if (!this.observe) {
|
113
|
-
return false;
|
114
|
-
}
|
115
|
-
const callbacks = await this.getCallbacks();
|
116
|
-
return callbacks.length > 0;
|
117
|
-
}
|
118
|
-
/**
|
119
|
-
* Add a callback to the custom callbacks list.
|
120
|
-
* @param callback The callback to add.
|
121
|
-
*/
|
122
|
-
addCallback(callback) {
|
123
|
-
if (!this.customCallbacks) {
|
124
|
-
this.customCallbacks = [];
|
125
|
-
}
|
126
|
-
this.customCallbacks.push(callback);
|
127
|
-
logger.debug(`ObservabilityManager: Added custom callback: ${callback.constructor.name}`);
|
128
|
-
}
|
129
|
-
/**
|
130
|
-
* Clear all custom callbacks.
|
131
|
-
*/
|
132
|
-
clearCallbacks() {
|
133
|
-
this.customCallbacks = [];
|
134
|
-
logger.debug('ObservabilityManager: Cleared all custom callbacks');
|
135
|
-
}
|
136
|
-
/**
|
137
|
-
* Flush all pending traces to observability platforms.
|
138
|
-
* Important for serverless environments and short-lived processes.
|
139
|
-
*/
|
140
|
-
async flush() {
|
141
|
-
// Flush Langfuse traces
|
142
|
-
const callbacks = await this.getCallbacks();
|
143
|
-
for (const callback of callbacks) {
|
144
|
-
if ('flushAsync' in callback && typeof callback.flushAsync === 'function') {
|
145
|
-
await callback.flushAsync();
|
146
|
-
}
|
147
|
-
}
|
148
|
-
logger.debug('ObservabilityManager: All traces flushed');
|
149
|
-
}
|
150
|
-
/**
|
151
|
-
* Shutdown all handlers gracefully (for serverless environments).
|
152
|
-
*/
|
153
|
-
async shutdown() {
|
154
|
-
// Flush before shutdown
|
155
|
-
await this.flush();
|
156
|
-
// Shutdown other callbacks
|
157
|
-
const callbacks = await this.getCallbacks();
|
158
|
-
for (const callback of callbacks) {
|
159
|
-
// Check if the callback has a shutdown method (like Langfuse)
|
160
|
-
if ('shutdownAsync' in callback && typeof callback.shutdownAsync === 'function') {
|
161
|
-
await callback.shutdownAsync();
|
162
|
-
}
|
163
|
-
else if ('shutdown' in callback && typeof callback.shutdown === 'function') {
|
164
|
-
await callback.shutdown();
|
165
|
-
}
|
166
|
-
}
|
167
|
-
logger.debug('ObservabilityManager: All handlers shutdown');
|
168
|
-
}
|
169
|
-
/**
|
170
|
-
* String representation of the ObservabilityManager.
|
171
|
-
*/
|
172
|
-
toString() {
|
173
|
-
const names = this.handlerNames;
|
174
|
-
if (names.length > 0) {
|
175
|
-
return `ObservabilityManager(handlers=${names.join(', ')})`;
|
176
|
-
}
|
177
|
-
return 'ObservabilityManager(no handlers)';
|
178
|
-
}
|
179
|
-
}
|
180
|
-
// Singleton instance for easy access
|
181
|
-
let defaultManager = null;
|
182
|
-
/**
|
183
|
-
* Get the default ObservabilityManager instance.
|
184
|
-
* @returns The default ObservabilityManager instance (singleton).
|
185
|
-
*/
|
186
|
-
export function getDefaultManager() {
|
187
|
-
if (!defaultManager) {
|
188
|
-
defaultManager = new ObservabilityManager();
|
189
|
-
}
|
190
|
-
return defaultManager;
|
191
|
-
}
|
192
|
-
/**
|
193
|
-
* Create a new ObservabilityManager instance.
|
194
|
-
* @param config Configuration options
|
195
|
-
* @returns A new ObservabilityManager instance.
|
196
|
-
*/
|
197
|
-
export function createManager(config = {}) {
|
198
|
-
return new ObservabilityManager(config);
|
199
|
-
}
|
package/dist/src/session.js
DELETED
@@ -1,23 +0,0 @@
|
|
1
|
-
export class MCPSession {
|
2
|
-
connector;
|
3
|
-
autoConnect;
|
4
|
-
constructor(connector, autoConnect = true) {
|
5
|
-
this.connector = connector;
|
6
|
-
this.autoConnect = autoConnect;
|
7
|
-
}
|
8
|
-
async connect() {
|
9
|
-
await this.connector.connect();
|
10
|
-
}
|
11
|
-
async disconnect() {
|
12
|
-
await this.connector.disconnect();
|
13
|
-
}
|
14
|
-
async initialize() {
|
15
|
-
if (!this.isConnected && this.autoConnect) {
|
16
|
-
await this.connect();
|
17
|
-
}
|
18
|
-
await this.connector.initialize();
|
19
|
-
}
|
20
|
-
get isConnected() {
|
21
|
-
return this.connector && this.connector.isClientConnected;
|
22
|
-
}
|
23
|
-
}
|
@@ -1,127 +0,0 @@
|
|
1
|
-
import { logger } from '../logging.js';
|
2
|
-
export class ConnectionManager {
|
3
|
-
_readyPromise;
|
4
|
-
_readyResolver;
|
5
|
-
_donePromise;
|
6
|
-
_doneResolver;
|
7
|
-
_exception = null;
|
8
|
-
_connection = null;
|
9
|
-
_task = null;
|
10
|
-
_abortController = null;
|
11
|
-
constructor() {
|
12
|
-
this.reset();
|
13
|
-
}
|
14
|
-
/**
|
15
|
-
* Start the connection manager and establish a connection.
|
16
|
-
*
|
17
|
-
* @returns The established connection.
|
18
|
-
* @throws If the connection cannot be established.
|
19
|
-
*/
|
20
|
-
async start() {
|
21
|
-
// Reset internal state before starting
|
22
|
-
this.reset();
|
23
|
-
logger.debug(`Starting ${this.constructor.name}`);
|
24
|
-
// Kick off the background task that manages the connection
|
25
|
-
this._task = this.connectionTask();
|
26
|
-
// Wait until the connection is ready or an error occurs
|
27
|
-
await this._readyPromise;
|
28
|
-
// If an exception occurred during startup, re‑throw it
|
29
|
-
if (this._exception) {
|
30
|
-
throw this._exception;
|
31
|
-
}
|
32
|
-
if (this._connection === null) {
|
33
|
-
throw new Error('Connection was not established');
|
34
|
-
}
|
35
|
-
return this._connection;
|
36
|
-
}
|
37
|
-
/**
|
38
|
-
* Stop the connection manager and close the connection.
|
39
|
-
*/
|
40
|
-
async stop() {
|
41
|
-
if (this._task && this._abortController) {
|
42
|
-
logger.debug(`Cancelling ${this.constructor.name} task`);
|
43
|
-
this._abortController.abort();
|
44
|
-
try {
|
45
|
-
await this._task;
|
46
|
-
}
|
47
|
-
catch (e) {
|
48
|
-
if (e instanceof Error && e.name === 'AbortError') {
|
49
|
-
logger.debug(`${this.constructor.name} task aborted successfully`);
|
50
|
-
}
|
51
|
-
else {
|
52
|
-
logger.warn(`Error stopping ${this.constructor.name} task: ${e}`);
|
53
|
-
}
|
54
|
-
}
|
55
|
-
}
|
56
|
-
// Wait until the connection cleanup has completed
|
57
|
-
await this._donePromise;
|
58
|
-
logger.debug(`${this.constructor.name} task completed`);
|
59
|
-
}
|
60
|
-
/**
|
61
|
-
* Reset all internal state.
|
62
|
-
*/
|
63
|
-
reset() {
|
64
|
-
this._readyPromise = new Promise(res => (this._readyResolver = res));
|
65
|
-
this._donePromise = new Promise(res => (this._doneResolver = res));
|
66
|
-
this._exception = null;
|
67
|
-
this._connection = null;
|
68
|
-
this._task = null;
|
69
|
-
this._abortController = new AbortController();
|
70
|
-
}
|
71
|
-
/**
|
72
|
-
* The background task responsible for establishing and maintaining the
|
73
|
-
* connection until it is cancelled.
|
74
|
-
*/
|
75
|
-
async connectionTask() {
|
76
|
-
logger.debug(`Running ${this.constructor.name} task`);
|
77
|
-
try {
|
78
|
-
// Establish the connection
|
79
|
-
this._connection = await this.establishConnection();
|
80
|
-
logger.debug(`${this.constructor.name} connected successfully`);
|
81
|
-
// Signal that the connection is ready
|
82
|
-
this._readyResolver();
|
83
|
-
// Keep the task alive until it is cancelled
|
84
|
-
await this.waitForAbort();
|
85
|
-
}
|
86
|
-
catch (err) {
|
87
|
-
this._exception = err;
|
88
|
-
logger.error(`Error in ${this.constructor.name} task: ${err}`);
|
89
|
-
// Ensure the ready promise resolves so that start() can handle the error
|
90
|
-
this._readyResolver();
|
91
|
-
}
|
92
|
-
finally {
|
93
|
-
// Clean up the connection if it was established
|
94
|
-
if (this._connection !== null) {
|
95
|
-
try {
|
96
|
-
await this.closeConnection(this._connection);
|
97
|
-
}
|
98
|
-
catch (closeErr) {
|
99
|
-
logger.warn(`Error closing connection in ${this.constructor.name}: ${closeErr}`);
|
100
|
-
}
|
101
|
-
this._connection = null;
|
102
|
-
}
|
103
|
-
// Signal that cleanup is finished
|
104
|
-
this._doneResolver();
|
105
|
-
}
|
106
|
-
}
|
107
|
-
/**
|
108
|
-
* Helper that returns a promise which resolves when the abort signal fires.
|
109
|
-
*/
|
110
|
-
async waitForAbort() {
|
111
|
-
return new Promise((_resolve, _reject) => {
|
112
|
-
if (!this._abortController) {
|
113
|
-
return;
|
114
|
-
}
|
115
|
-
const signal = this._abortController.signal;
|
116
|
-
if (signal.aborted) {
|
117
|
-
_resolve();
|
118
|
-
return;
|
119
|
-
}
|
120
|
-
const onAbort = () => {
|
121
|
-
signal.removeEventListener('abort', onAbort);
|
122
|
-
_resolve();
|
123
|
-
};
|
124
|
-
signal.addEventListener('abort', onAbort);
|
125
|
-
});
|
126
|
-
}
|
127
|
-
}
|
@@ -1,5 +0,0 @@
|
|
1
|
-
export { ConnectionManager } from './base.js';
|
2
|
-
export { SseConnectionManager } from './sse.js';
|
3
|
-
export { StdioConnectionManager } from './stdio.js';
|
4
|
-
export { StreamableHttpConnectionManager } from './streamable_http.js';
|
5
|
-
export { WebSocketConnectionManager } from './websocket.js';
|
@@ -1,43 +0,0 @@
|
|
1
|
-
import { SSEClientTransport } from '@modelcontextprotocol/sdk/client/sse.js';
|
2
|
-
import { logger } from '../logging.js';
|
3
|
-
import { ConnectionManager } from './base.js';
|
4
|
-
export class SseConnectionManager extends ConnectionManager {
|
5
|
-
url;
|
6
|
-
opts;
|
7
|
-
_transport = null;
|
8
|
-
/**
|
9
|
-
* Create an SSE connection manager.
|
10
|
-
*
|
11
|
-
* @param url The SSE endpoint URL.
|
12
|
-
* @param opts Optional transport options (auth, headers, etc.).
|
13
|
-
*/
|
14
|
-
constructor(url, opts) {
|
15
|
-
super();
|
16
|
-
this.url = typeof url === 'string' ? new URL(url) : url;
|
17
|
-
this.opts = opts;
|
18
|
-
}
|
19
|
-
/**
|
20
|
-
* Spawn a new `SSEClientTransport` and start the connection.
|
21
|
-
*/
|
22
|
-
async establishConnection() {
|
23
|
-
this._transport = new SSEClientTransport(this.url, this.opts);
|
24
|
-
logger.debug(`${this.constructor.name} connected successfully`);
|
25
|
-
return this._transport;
|
26
|
-
}
|
27
|
-
/**
|
28
|
-
* Close the underlying transport and clean up resources.
|
29
|
-
*/
|
30
|
-
async closeConnection(_connection) {
|
31
|
-
if (this._transport) {
|
32
|
-
try {
|
33
|
-
await this._transport.close();
|
34
|
-
}
|
35
|
-
catch (e) {
|
36
|
-
logger.warn(`Error closing SSE transport: ${e}`);
|
37
|
-
}
|
38
|
-
finally {
|
39
|
-
this._transport = null;
|
40
|
-
}
|
41
|
-
}
|
42
|
-
}
|
43
|
-
}
|
@@ -1,51 +0,0 @@
|
|
1
|
-
import { StdioClientTransport } from '@modelcontextprotocol/sdk/client/stdio.js';
|
2
|
-
import { logger } from '../logging.js';
|
3
|
-
import { ConnectionManager } from './base.js';
|
4
|
-
export class StdioConnectionManager extends ConnectionManager {
|
5
|
-
serverParams;
|
6
|
-
errlog;
|
7
|
-
_transport = null;
|
8
|
-
/**
|
9
|
-
* Create a new stdio connection manager.
|
10
|
-
*
|
11
|
-
* @param serverParams Parameters for the stdio server process.
|
12
|
-
* @param errlog Stream to which the server's stderr should be piped.
|
13
|
-
* Defaults to `process.stderr`.
|
14
|
-
*/
|
15
|
-
constructor(serverParams, errlog = process.stderr) {
|
16
|
-
super();
|
17
|
-
this.serverParams = serverParams;
|
18
|
-
this.errlog = errlog;
|
19
|
-
}
|
20
|
-
/**
|
21
|
-
* Establish the stdio connection by spawning the server process and starting
|
22
|
-
* the SDK's transport. Returns the live `StdioClientTransport` instance.
|
23
|
-
*/
|
24
|
-
async establishConnection() {
|
25
|
-
// Instantiate and start the transport
|
26
|
-
this._transport = new StdioClientTransport(this.serverParams);
|
27
|
-
// If stderr was piped, forward it to `errlog` for visibility
|
28
|
-
if (this._transport.stderr && typeof this._transport.stderr.pipe === 'function') {
|
29
|
-
this._transport.stderr.pipe(this.errlog);
|
30
|
-
}
|
31
|
-
logger.debug(`${this.constructor.name} connected successfully`);
|
32
|
-
return this._transport;
|
33
|
-
}
|
34
|
-
/**
|
35
|
-
* Close the stdio connection, making sure the transport cleans up the child
|
36
|
-
* process and associated resources.
|
37
|
-
*/
|
38
|
-
async closeConnection(_connection) {
|
39
|
-
if (this._transport) {
|
40
|
-
try {
|
41
|
-
await this._transport.close();
|
42
|
-
}
|
43
|
-
catch (e) {
|
44
|
-
logger.warn(`Error closing stdio transport: ${e}`);
|
45
|
-
}
|
46
|
-
finally {
|
47
|
-
this._transport = null;
|
48
|
-
}
|
49
|
-
}
|
50
|
-
}
|
51
|
-
}
|