Skip to content
This repository was archived by the owner on May 21, 2026. It is now read-only.

Commit 026375c

Browse files
committed
feat: add observability example and enhance README documentation
- Introduced a new example for observability integration in mcp_everything.ts - Updated README to streamline observability setup instructions and remove redundant sections - Added support for an 'observe' option in MCPAgent and ObservabilityManager to control observability behavior - Improved observability manager to handle cases when observability is disabled - Cleaned up example code for better clarity and removed unnecessary debug logging
1 parent 7ce8257 commit 026375c

7 files changed

Lines changed: 101 additions & 173 deletions

File tree

README.md

Lines changed: 5 additions & 140 deletions
Original file line numberDiff line numberDiff line change
@@ -298,25 +298,9 @@ export function Chat() {
298298

299299
## 📊 Observability & Monitoring
300300

301-
mcp-use-ts provides built-in observability support through the `ObservabilityManager`, with seamless integration for Langfuse and other observability platforms.
301+
mcp-use-ts provides built-in observability support through the `ObservabilityManager`, with integration for Langfuse and other observability platforms.
302302

303-
### Features
304-
305-
- **Automatic Tracing**: All agent executions, tool calls, and LLM interactions are automatically traced
306-
- **Dynamic Metadata**: Add contextual metadata to traces at runtime
307-
- **Tag Support**: Tag traces with dynamic information for better organization
308-
- **Langfuse Integration**: Native support for Langfuse observability platform
309-
- **Custom Callbacks**: Support for additional custom callback handlers
310-
311-
### Setup
312-
313-
#### 1. Install Observability Dependencies
314-
315-
```bash
316-
npm install langfuse langfuse-langchain
317-
```
318-
319-
#### 2. Configure Environment Variables
303+
#### To enable observability simply configure Environment Variables
320304

321305
```ini
322306
# .env
@@ -325,41 +309,12 @@ LANGFUSE_SECRET_KEY=sk-lf-your-secret-key
325309
LANGFUSE_HOST=https://cloud.langfuse.com # or your self-hosted instance
326310
```
327311

328-
#### 3. Basic Usage
329-
330-
```ts
331-
import { ChatOpenAI } from '@langchain/openai'
332-
import { MCPAgent, MCPClient, ObservabilityManager } from 'mcp-use'
333-
334-
const client = new MCPClient({
335-
mcpServers: {
336-
everything: { command: 'npx', args: ['-y', '@modelcontextprotocol/server-everything'] }
337-
}
338-
})
339-
340-
const llm = new ChatOpenAI({ model: 'gpt-4o' })
341-
342-
// Create agent with observability enabled
343-
const agent = new MCPAgent({
344-
llm,
345-
client,
346-
maxSteps: 10,
347-
verbose: true // Enable verbose logging for better observability
348-
})
349-
350-
// Initialize to set up observability callbacks
351-
await agent.initialize()
352-
353-
// Run queries - all interactions will be automatically traced
354-
const result = await agent.run('What tools are available?')
355-
```
356-
357312
### Advanced Observability Features
358313

359314
#### Dynamic Metadata and Tags
360315

361316
```ts
362-
// Set metadata for the current execution
317+
// Set custom metadata for the current execution
363318
agent.setMetadata({
364319
userId: 'user123',
365320
sessionId: 'session456',
@@ -373,77 +328,6 @@ agent.setTags(['production', 'user-query', 'tool-discovery'])
373328
const result = await agent.run('Search for restaurants in Tokyo')
374329
```
375330

376-
#### Custom Observability Manager
377-
378-
```ts
379-
import { ObservabilityManager } from 'mcp-use'
380-
381-
// Create custom observability manager
382-
const observabilityManager = new ObservabilityManager({
383-
verbose: true,
384-
agentId: 'my-custom-agent',
385-
metadata: {
386-
version: '1.0.0',
387-
deployment: 'production'
388-
},
389-
metadataProvider: () => ({
390-
timestamp: new Date().toISOString(),
391-
requestId: crypto.randomUUID()
392-
}),
393-
tagsProvider: () => ['automated', 'scheduled']
394-
})
395-
396-
// Use with MCPAgent
397-
const agent = new MCPAgent({
398-
llm,
399-
client,
400-
callbacks: await observabilityManager.getCallbacks()
401-
})
402-
```
403-
404-
#### Runtime Metadata Updates
405-
406-
```ts
407-
// Update metadata during execution
408-
agent.setMetadata({
409-
currentStep: 1,
410-
toolsUsed: ['search', 'browse']
411-
})
412-
413-
// Add more tags
414-
agent.setTags(['step-1', 'search-complete'])
415-
416-
// Continue execution with updated context
417-
const result = await agent.run('Continue with the search')
418-
```
419-
420-
### Observability in Production
421-
422-
#### Environment Configuration
423-
424-
```ts
425-
// Production setup with comprehensive observability
426-
const agent = new MCPAgent({
427-
llm,
428-
client,
429-
maxSteps: 20,
430-
verbose: process.env.NODE_ENV === 'development',
431-
// Observability is automatically enabled when environment variables are set
432-
})
433-
434-
await agent.initialize()
435-
436-
// Set production metadata
437-
agent.setMetadata({
438-
service: 'mcp-agent',
439-
version: process.env.APP_VERSION,
440-
environment: process.env.NODE_ENV,
441-
region: process.env.AWS_REGION
442-
})
443-
444-
agent.setTags(['production', 'api', 'mcp-agent'])
445-
```
446-
447331
#### Monitoring Agent Performance
448332

449333
```ts
@@ -471,32 +355,13 @@ for await (const event of eventStream) {
471355

472356
### Disabling Observability
473357

474-
To disable observability, set the environment variable:
475-
476-
```ini
477-
MCP_USE_LANGFUSE=false
478-
```
479-
480-
Or programmatically:
358+
To disable observability, either remove langfuse env variables or
481359

482360
```ts
483361
const agent = new MCPAgent({
484362
llm,
485363
client,
486-
callbacks: [] // No callbacks = no observability
487-
})
488-
```
489-
490-
### Available Observability Exports
491-
492-
```ts
493-
import type { ObservabilityConfig } from 'mcp-use'
494-
import { ObservabilityManager } from 'mcp-use'
495-
496-
// Use ObservabilityManager directly for custom setups
497-
const manager = new ObservabilityManager({
498-
verbose: true,
499-
agentId: 'custom-agent'
364+
observe: false
500365
})
501366
```
502367

examples/mcp_everything.ts

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,28 @@
55

66
import { ChatOpenAI } from '@langchain/openai'
77
import { config } from 'dotenv'
8-
import { Logger, MCPAgent, MCPClient } from '../index.js'
8+
import { MCPAgent, MCPClient } from '../index.js'
99

1010
// Load environment variables from .env file
1111
config()
1212

13-
// Enable debug logging to see observability messages
14-
Logger.setDebug(true)
15-
1613
const everythingServer = {
1714
mcpServers: { everything: { command: 'npx', args: ['-y', '@modelcontextprotocol/server-everything'] } },
1815
}
1916

2017
async function main() {
21-
console.log('🚀 Starting MCP Everything example with Langfuse tracing...')
22-
console.log('📊 Environment variables:')
23-
console.log(` LANGFUSE_PUBLIC_KEY: ${process.env.LANGFUSE_PUBLIC_KEY ? '✅ Set' : '❌ Missing'}`)
24-
console.log(` LANGFUSE_SECRET_KEY: ${process.env.LANGFUSE_SECRET_KEY ? '✅ Set' : '❌ Missing'}`)
25-
console.log(` LANGFUSE_HOST: ${process.env.LANGFUSE_HOST || 'Not set'}`)
26-
console.log(` MCP_USE_LANGFUSE: ${process.env.MCP_USE_LANGFUSE || 'Not set'}`)
27-
2818
const client = new MCPClient(everythingServer)
2919
const llm = new ChatOpenAI({ model: 'gpt-4o', temperature: 0 })
30-
const agent = new MCPAgent({
31-
llm,
32-
client,
33-
maxSteps: 30,
34-
})
35-
36-
console.log('🔧 Initializing agent...')
37-
await agent.initialize()
20+
const agent = new MCPAgent({ llm, client, maxSteps: 30 })
3821

39-
// Set additional metadata for testing
40-
agent.setMetadata({
41-
agent_id: 'test-agent-123',
42-
test_run: true,
43-
example: 'mcp_everything',
44-
})
45-
46-
console.log('💬 Running agent query...')
4722
const result = await agent.run(
4823
`Hello, you are a tester can you please answer the follwing questions:
4924
- Which resources do you have access to?
5025
- Which prompts do you have access to?
5126
- Which tools do you have access to?`,
5227
30,
5328
)
54-
console.log(`\n✅ Result: ${result}`)
55-
56-
console.log('🧹 Closing agent...')
57-
await agent.close()
58-
console.log('🎉 Example completed! Check your Langfuse dashboard for traces.')
29+
console.log(`\nResult: ${result}`)
5930
}
6031

6132
if (import.meta.url === `file://${process.argv[1]}`) {

examples/observability.ts

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
/**
2+
* This example shows how to test the different functionalities of MCPs using the MCP server from
3+
* anthropic.
4+
*/
5+
6+
import { ChatOpenAI } from '@langchain/openai'
7+
import { config } from 'dotenv'
8+
import { Logger, MCPAgent, MCPClient } from '../index.js'
9+
10+
// Load environment variables from .env file
11+
config()
12+
13+
// Enable debug logging to see observability messages
14+
Logger.setDebug(true)
15+
16+
const everythingServer = {
17+
mcpServers: { everything: { command: 'npx', args: ['-y', '@modelcontextprotocol/server-everything'] } },
18+
}
19+
20+
async function main() {
21+
console.log('🚀 Starting MCP Observability example with Langfuse tracing...')
22+
console.log('📊 Environment variables:')
23+
console.log(` LANGFUSE_PUBLIC_KEY: ${process.env.LANGFUSE_PUBLIC_KEY ? '✅ Set' : '❌ Missing'}`)
24+
console.log(` LANGFUSE_SECRET_KEY: ${process.env.LANGFUSE_SECRET_KEY ? '✅ Set' : '❌ Missing'}`)
25+
console.log(` LANGFUSE_HOST: ${process.env.LANGFUSE_HOST || 'Not set'}`)
26+
console.log(` MCP_USE_LANGFUSE: ${process.env.MCP_USE_LANGFUSE || 'Not set'}`)
27+
28+
const client = new MCPClient(everythingServer)
29+
const llm = new ChatOpenAI({ model: 'gpt-4o', temperature: 0 })
30+
const agent = new MCPAgent({
31+
llm,
32+
client,
33+
maxSteps: 30,
34+
})
35+
36+
// console.log('🔧 Initializing agent...')
37+
// await agent.initialize()
38+
39+
// Set additional metadata for testing (Optional)
40+
agent.setMetadata({
41+
agent_id: 'test-agent-123',
42+
test_run: true,
43+
example: 'mcp_observability',
44+
})
45+
46+
agent.setTags(['test-tag-1', 'test-tag-2'])
47+
48+
console.log('💬 Running agent query...')
49+
const result = await agent.run(
50+
`Hello, you are a tester can you please answer the follwing questions:
51+
- Which resources do you have access to?
52+
- Which prompts do you have access to?
53+
- Which tools do you have access to?`,
54+
30,
55+
)
56+
console.log(`\n✅ Result: ${result}`)
57+
58+
// console.log('🧹 Closing agent...')
59+
// await agent.close()
60+
// console.log('🎉 Example completed! Check your Langfuse dashboard for traces.')
61+
}
62+
63+
if (import.meta.url === `file://${process.argv[1]}`) {
64+
main().catch(console.error)
65+
}

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@
7474
"example:oauth": "npm run build && node dist/examples/simple_oauth_example.js",
7575
"example:blender": "npm run build && node dist/examples/blender_use.js",
7676
"example:add_server": "npm run build && node dist/examples/add_server_tool.js",
77-
"example:structured": "npm run build && node dist/examples/structured_output.js"
77+
"example:structured": "npm run build && node dist/examples/structured_output.js",
78+
"example:observability": "npm run build && node dist/examples/observability.js"
7879
},
7980
"peerDependencies": {
8081
"langfuse": "^3.32.0",

src/agents/mcp_agent.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export class MCPAgent {
4646
private additionalTools: StructuredToolInterface[]
4747
private useServerManager: boolean
4848
private verbose: boolean
49+
private observe: boolean
4950
private systemPrompt?: string | null
5051
private systemPromptTemplateOverride?: string | null
5152
private additionalInstructions?: string | null
@@ -86,6 +87,7 @@ export class MCPAgent {
8687
additionalTools?: StructuredToolInterface[]
8788
useServerManager?: boolean
8889
verbose?: boolean
90+
observe?: boolean
8991
adapter?: LangChainAdapter
9092
serverManagerFactory?: (client: MCPClient) => ServerManager
9193
callbacks?: BaseCallbackHandler[]
@@ -107,6 +109,7 @@ export class MCPAgent {
107109
this.memoryEnabled = options.memoryEnabled ?? true
108110
this.autoInitialize = options.autoInitialize ?? false
109111
this.verbose = options.verbose ?? false
112+
this.observe = options.observe ?? true
110113
this.connectors = []
111114
this.disallowedTools = []
112115
this.additionalTools = []
@@ -142,6 +145,7 @@ export class MCPAgent {
142145
this.additionalTools = options.additionalTools ?? []
143146
this.useServerManager = options.useServerManager ?? false
144147
this.verbose = options.verbose ?? false
148+
this.observe = options.observe ?? true
145149

146150
if (!this.client && this.connectors.length === 0) {
147151
throw new Error('Either \'client\' or at least one \'connector\' must be provided.')
@@ -176,6 +180,7 @@ export class MCPAgent {
176180
this.observabilityManager = new ObservabilityManager({
177181
customCallbacks: options.callbacks,
178182
verbose: this.verbose,
183+
observe: this.observe,
179184
agentId: options.agentId,
180185
metadataProvider: () => this.getMetadata(),
181186
tagsProvider: () => this.getTags(),

0 commit comments

Comments
 (0)