@mastra/mcp 0.3.10-alpha.4 → 0.3.10-alpha.6
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/.turbo/turbo-build.log +7 -7
- package/CHANGELOG.md +14 -0
- package/README.md +93 -0
- package/dist/_tsup-dts-rollup.d.cts +43 -11
- package/dist/_tsup-dts-rollup.d.ts +43 -11
- package/dist/index.cjs +4143 -14
- package/dist/index.d.cts +4 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +4143 -14
- package/package.json +2 -2
- package/src/client.ts +119 -17
- package/src/configuration.ts +11 -6
- package/src/server-logging.test.ts +189 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
1
|
|
|
2
|
-
> @mastra/mcp@0.3.10-alpha.
|
|
2
|
+
> @mastra/mcp@0.3.10-alpha.6 build /home/runner/work/mastra/mastra/packages/mcp
|
|
3
3
|
> tsup src/index.ts --format esm,cjs --experimental-dts --clean --treeshake=smallest --splitting
|
|
4
4
|
|
|
5
5
|
[34mCLI[39m Building entry: src/index.ts
|
|
6
6
|
[34mCLI[39m Using tsconfig: tsconfig.json
|
|
7
7
|
[34mCLI[39m tsup v8.4.0
|
|
8
8
|
[34mTSC[39m Build start
|
|
9
|
-
[32mTSC[39m ⚡️ Build success in
|
|
9
|
+
[32mTSC[39m ⚡️ Build success in 14601ms
|
|
10
10
|
[34mDTS[39m Build start
|
|
11
11
|
[34mCLI[39m Target: es2022
|
|
12
12
|
Analysis will use the bundled TypeScript version 5.8.2
|
|
13
13
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.ts[39m
|
|
14
14
|
Analysis will use the bundled TypeScript version 5.8.2
|
|
15
15
|
[36mWriting package typings: /home/runner/work/mastra/mastra/packages/mcp/dist/_tsup-dts-rollup.d.cts[39m
|
|
16
|
-
[32mDTS[39m ⚡️ Build success in
|
|
16
|
+
[32mDTS[39m ⚡️ Build success in 10596ms
|
|
17
17
|
[34mCLI[39m Cleaning output folder
|
|
18
18
|
[34mESM[39m Build start
|
|
19
19
|
[34mCJS[39m Build start
|
|
20
|
-
[
|
|
21
|
-
[
|
|
22
|
-
[
|
|
23
|
-
[
|
|
20
|
+
[32mESM[39m [1mdist/index.js [22m[32m128.98 KB[39m
|
|
21
|
+
[32mESM[39m ⚡️ Build success in 2583ms
|
|
22
|
+
[32mCJS[39m [1mdist/index.cjs [22m[32m129.02 KB[39m
|
|
23
|
+
[32mCJS[39m ⚡️ Build success in 2583ms
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
# @mastra/mcp
|
|
2
2
|
|
|
3
|
+
## 0.3.10-alpha.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- Updated dependencies [d72318f]
|
|
8
|
+
- @mastra/core@0.8.3-alpha.5
|
|
9
|
+
|
|
10
|
+
## 0.3.10-alpha.5
|
|
11
|
+
|
|
12
|
+
### Patch Changes
|
|
13
|
+
|
|
14
|
+
- Updated dependencies [7f1b291]
|
|
15
|
+
- @mastra/core@0.8.3-alpha.4
|
|
16
|
+
|
|
3
17
|
## 0.3.10-alpha.4
|
|
4
18
|
|
|
5
19
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -95,6 +95,97 @@ const tools = await mcp.getTools();
|
|
|
95
95
|
const toolsets = await mcp.getToolsets();
|
|
96
96
|
```
|
|
97
97
|
|
|
98
|
+
## Logging
|
|
99
|
+
|
|
100
|
+
The MCP client provides per-server logging capabilities, allowing you to monitor interactions with each MCP server separately:
|
|
101
|
+
|
|
102
|
+
```typescript
|
|
103
|
+
import { MCPConfiguration, LogMessage, LoggingLevel } from '@mastra/mcp';
|
|
104
|
+
|
|
105
|
+
// Define a custom log handler
|
|
106
|
+
const weatherLogger = (logMessage: LogMessage) => {
|
|
107
|
+
console.log(`[${logMessage.level}] ${logMessage.serverName}: ${logMessage.message}`);
|
|
108
|
+
|
|
109
|
+
// Log data contains valuable information
|
|
110
|
+
console.log('Details:', logMessage.details);
|
|
111
|
+
console.log('Timestamp:', logMessage.timestamp);
|
|
112
|
+
};
|
|
113
|
+
|
|
114
|
+
// Initialize MCP configuration with server-specific loggers
|
|
115
|
+
const mcp = new MCPConfiguration({
|
|
116
|
+
servers: {
|
|
117
|
+
weatherService: {
|
|
118
|
+
command: 'npx',
|
|
119
|
+
args: ['tsx', 'weather-mcp.ts'],
|
|
120
|
+
// Attach the logger to this specific server
|
|
121
|
+
log: weatherLogger,
|
|
122
|
+
},
|
|
123
|
+
|
|
124
|
+
stockPriceService: {
|
|
125
|
+
command: 'npx',
|
|
126
|
+
args: ['tsx', 'stock-mcp.ts'],
|
|
127
|
+
// Different logger for this service
|
|
128
|
+
log: logMessage => {
|
|
129
|
+
// Just log errors and critical events for this service
|
|
130
|
+
if (['error', 'critical', 'alert', 'emergency'].includes(logMessage.level)) {
|
|
131
|
+
console.error(`Stock service ${logMessage.level}: ${logMessage.message}`);
|
|
132
|
+
}
|
|
133
|
+
},
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
});
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Log Message Structure
|
|
140
|
+
|
|
141
|
+
Each log message contains the following information:
|
|
142
|
+
|
|
143
|
+
```typescript
|
|
144
|
+
interface LogMessage {
|
|
145
|
+
level: LoggingLevel; // MCP SDK standard log levels
|
|
146
|
+
message: string;
|
|
147
|
+
timestamp: Date;
|
|
148
|
+
serverName: string;
|
|
149
|
+
details?: Record<string, any>;
|
|
150
|
+
}
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
The `LoggingLevel` type is directly imported from the MCP SDK, ensuring compatibility with all standard MCP log levels: `'debug' | 'info' | 'notice' | 'warning' | 'error' | 'critical' | 'alert' | 'emergency'`.
|
|
154
|
+
|
|
155
|
+
### Creating Reusable Loggers
|
|
156
|
+
|
|
157
|
+
You can create reusable logger factories for common patterns:
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// File logger factory with color coded output for different severity levels
|
|
161
|
+
const createFileLogger = filePath => {
|
|
162
|
+
return logMessage => {
|
|
163
|
+
// Format the message based on level
|
|
164
|
+
const prefix =
|
|
165
|
+
logMessage.level === 'emergency' ? '!!! EMERGENCY !!! ' : logMessage.level === 'alert' ? '! ALERT ! ' : '';
|
|
166
|
+
|
|
167
|
+
// Write to file with timestamp, level, etc.
|
|
168
|
+
fs.appendFileSync(
|
|
169
|
+
filePath,
|
|
170
|
+
`[${logMessage.timestamp.toISOString()}] [${logMessage.level.toUpperCase()}] ${prefix}${logMessage.message}\n`,
|
|
171
|
+
);
|
|
172
|
+
};
|
|
173
|
+
};
|
|
174
|
+
|
|
175
|
+
// Use the factory in configuration
|
|
176
|
+
const mcp = new MCPConfiguration({
|
|
177
|
+
servers: {
|
|
178
|
+
weatherService: {
|
|
179
|
+
command: 'npx',
|
|
180
|
+
args: ['tsx', 'weather-mcp.ts'],
|
|
181
|
+
log: createFileLogger('./logs/weather.log'),
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
});
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
See the `examples/server-logging.ts` file for comprehensive examples of various logging strategies.
|
|
188
|
+
|
|
98
189
|
### Tools vs Toolsets
|
|
99
190
|
|
|
100
191
|
The MCPConfiguration class provides two ways to access MCP tools:
|
|
@@ -242,6 +333,7 @@ This configuration ensures that:
|
|
|
242
333
|
|
|
243
334
|
- `version`: Client version (default: '1.0.0')
|
|
244
335
|
- `capabilities`: ClientCapabilities object for specifying supported features
|
|
336
|
+
- `log`: Function that receives and processes log messages
|
|
245
337
|
|
|
246
338
|
## Features
|
|
247
339
|
|
|
@@ -251,6 +343,7 @@ This configuration ensures that:
|
|
|
251
343
|
- Multiple transport layers:
|
|
252
344
|
- Stdio-based for local servers
|
|
253
345
|
- SSE-based for remote servers
|
|
346
|
+
- Per-server logging capability using all standard MCP log levels
|
|
254
347
|
- Automatic error handling and logging
|
|
255
348
|
- Tool execution with context
|
|
256
349
|
|
|
@@ -1,15 +1,35 @@
|
|
|
1
1
|
import type { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
|
|
2
3
|
import { MastraBase } from '@mastra/core/base';
|
|
3
4
|
import type { Protocol } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
4
5
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
5
6
|
import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
6
7
|
import type { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
7
8
|
|
|
9
|
+
export { LoggingLevel }
|
|
10
|
+
export { LoggingLevel as LoggingLevel_alias_1 }
|
|
11
|
+
|
|
12
|
+
declare type LogHandler = (logMessage: LogMessage) => void;
|
|
13
|
+
export { LogHandler }
|
|
14
|
+
export { LogHandler as LogHandler_alias_1 }
|
|
15
|
+
|
|
16
|
+
declare interface LogMessage {
|
|
17
|
+
level: LoggingLevel;
|
|
18
|
+
message: string;
|
|
19
|
+
timestamp: Date;
|
|
20
|
+
serverName: string;
|
|
21
|
+
details?: Record<string, any>;
|
|
22
|
+
}
|
|
23
|
+
export { LogMessage }
|
|
24
|
+
export { LogMessage as LogMessage_alias_1 }
|
|
25
|
+
|
|
8
26
|
declare class MastraMCPClient extends MastraBase {
|
|
9
27
|
name: string;
|
|
10
28
|
private transport;
|
|
11
29
|
private client;
|
|
12
30
|
private readonly timeout;
|
|
31
|
+
private logHandler?;
|
|
32
|
+
private enableServerLogs?;
|
|
13
33
|
constructor({ name, version, server, capabilities, timeout, }: {
|
|
14
34
|
name: string;
|
|
15
35
|
server: MastraMCPServerDefinition;
|
|
@@ -17,6 +37,14 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
17
37
|
version?: string;
|
|
18
38
|
timeout?: number;
|
|
19
39
|
});
|
|
40
|
+
/**
|
|
41
|
+
* Log a message at the specified level
|
|
42
|
+
* @param level Log level
|
|
43
|
+
* @param message Log message
|
|
44
|
+
* @param details Optional additional details
|
|
45
|
+
*/
|
|
46
|
+
private log;
|
|
47
|
+
private setupLogging;
|
|
20
48
|
private isConnected;
|
|
21
49
|
connect(): Promise<void>;
|
|
22
50
|
disconnect(): Promise<void>;
|
|
@@ -26,7 +54,12 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
26
54
|
export { MastraMCPClient }
|
|
27
55
|
export { MastraMCPClient as MastraMCPClient_alias_1 }
|
|
28
56
|
|
|
29
|
-
declare type MastraMCPServerDefinition =
|
|
57
|
+
declare type MastraMCPServerDefinition = (StdioServerParameters | SSEClientParameters) & {
|
|
58
|
+
logger?: LogHandler;
|
|
59
|
+
timeout?: number;
|
|
60
|
+
capabilities?: ClientCapabilities;
|
|
61
|
+
enableServerLogs?: boolean;
|
|
62
|
+
};
|
|
30
63
|
export { MastraMCPServerDefinition }
|
|
31
64
|
export { MastraMCPServerDefinition as MastraMCPServerDefinition_alias_1 }
|
|
32
65
|
|
|
@@ -34,11 +67,7 @@ declare class MCPConfiguration extends MastraBase {
|
|
|
34
67
|
private serverConfigs;
|
|
35
68
|
private id;
|
|
36
69
|
private defaultTimeout;
|
|
37
|
-
constructor(args:
|
|
38
|
-
id?: string;
|
|
39
|
-
servers: Record<string, MastraMCPServerDefinition>;
|
|
40
|
-
timeout?: number;
|
|
41
|
-
});
|
|
70
|
+
constructor(args: MCPConfigurationOptions);
|
|
42
71
|
private addToInstanceCache;
|
|
43
72
|
private makeId;
|
|
44
73
|
disconnect(): Promise<void>;
|
|
@@ -51,6 +80,14 @@ declare class MCPConfiguration extends MastraBase {
|
|
|
51
80
|
export { MCPConfiguration }
|
|
52
81
|
export { MCPConfiguration as MCPConfiguration_alias_1 }
|
|
53
82
|
|
|
83
|
+
declare interface MCPConfigurationOptions {
|
|
84
|
+
id?: string;
|
|
85
|
+
servers: Record<string, MastraMCPServerDefinition>;
|
|
86
|
+
timeout?: number;
|
|
87
|
+
}
|
|
88
|
+
export { MCPConfigurationOptions }
|
|
89
|
+
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
|
|
90
|
+
|
|
54
91
|
export declare const server: Server<{
|
|
55
92
|
method: string;
|
|
56
93
|
params?: {
|
|
@@ -101,11 +138,6 @@ export declare const server_alias_1: Server<{
|
|
|
101
138
|
|
|
102
139
|
declare type SSEClientParameters = {
|
|
103
140
|
url: URL;
|
|
104
|
-
timeout?: number;
|
|
105
141
|
} & SSEClientTransportOptions;
|
|
106
142
|
|
|
107
|
-
declare type StdioServerParametersWithTimeout = StdioServerParameters & {
|
|
108
|
-
timeout?: number;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
143
|
export { }
|
|
@@ -1,15 +1,35 @@
|
|
|
1
1
|
import type { ClientCapabilities } from '@modelcontextprotocol/sdk/types.js';
|
|
2
|
+
import { LoggingLevel } from '@modelcontextprotocol/sdk/types.js';
|
|
2
3
|
import { MastraBase } from '@mastra/core/base';
|
|
3
4
|
import type { Protocol } from '@modelcontextprotocol/sdk/shared/protocol.js';
|
|
4
5
|
import { Server } from '@modelcontextprotocol/sdk/server/index.js';
|
|
5
6
|
import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
6
7
|
import type { StdioServerParameters } from '@modelcontextprotocol/sdk/client/stdio.js';
|
|
7
8
|
|
|
9
|
+
export { LoggingLevel }
|
|
10
|
+
export { LoggingLevel as LoggingLevel_alias_1 }
|
|
11
|
+
|
|
12
|
+
declare type LogHandler = (logMessage: LogMessage) => void;
|
|
13
|
+
export { LogHandler }
|
|
14
|
+
export { LogHandler as LogHandler_alias_1 }
|
|
15
|
+
|
|
16
|
+
declare interface LogMessage {
|
|
17
|
+
level: LoggingLevel;
|
|
18
|
+
message: string;
|
|
19
|
+
timestamp: Date;
|
|
20
|
+
serverName: string;
|
|
21
|
+
details?: Record<string, any>;
|
|
22
|
+
}
|
|
23
|
+
export { LogMessage }
|
|
24
|
+
export { LogMessage as LogMessage_alias_1 }
|
|
25
|
+
|
|
8
26
|
declare class MastraMCPClient extends MastraBase {
|
|
9
27
|
name: string;
|
|
10
28
|
private transport;
|
|
11
29
|
private client;
|
|
12
30
|
private readonly timeout;
|
|
31
|
+
private logHandler?;
|
|
32
|
+
private enableServerLogs?;
|
|
13
33
|
constructor({ name, version, server, capabilities, timeout, }: {
|
|
14
34
|
name: string;
|
|
15
35
|
server: MastraMCPServerDefinition;
|
|
@@ -17,6 +37,14 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
17
37
|
version?: string;
|
|
18
38
|
timeout?: number;
|
|
19
39
|
});
|
|
40
|
+
/**
|
|
41
|
+
* Log a message at the specified level
|
|
42
|
+
* @param level Log level
|
|
43
|
+
* @param message Log message
|
|
44
|
+
* @param details Optional additional details
|
|
45
|
+
*/
|
|
46
|
+
private log;
|
|
47
|
+
private setupLogging;
|
|
20
48
|
private isConnected;
|
|
21
49
|
connect(): Promise<void>;
|
|
22
50
|
disconnect(): Promise<void>;
|
|
@@ -26,7 +54,12 @@ declare class MastraMCPClient extends MastraBase {
|
|
|
26
54
|
export { MastraMCPClient }
|
|
27
55
|
export { MastraMCPClient as MastraMCPClient_alias_1 }
|
|
28
56
|
|
|
29
|
-
declare type MastraMCPServerDefinition =
|
|
57
|
+
declare type MastraMCPServerDefinition = (StdioServerParameters | SSEClientParameters) & {
|
|
58
|
+
logger?: LogHandler;
|
|
59
|
+
timeout?: number;
|
|
60
|
+
capabilities?: ClientCapabilities;
|
|
61
|
+
enableServerLogs?: boolean;
|
|
62
|
+
};
|
|
30
63
|
export { MastraMCPServerDefinition }
|
|
31
64
|
export { MastraMCPServerDefinition as MastraMCPServerDefinition_alias_1 }
|
|
32
65
|
|
|
@@ -34,11 +67,7 @@ declare class MCPConfiguration extends MastraBase {
|
|
|
34
67
|
private serverConfigs;
|
|
35
68
|
private id;
|
|
36
69
|
private defaultTimeout;
|
|
37
|
-
constructor(args:
|
|
38
|
-
id?: string;
|
|
39
|
-
servers: Record<string, MastraMCPServerDefinition>;
|
|
40
|
-
timeout?: number;
|
|
41
|
-
});
|
|
70
|
+
constructor(args: MCPConfigurationOptions);
|
|
42
71
|
private addToInstanceCache;
|
|
43
72
|
private makeId;
|
|
44
73
|
disconnect(): Promise<void>;
|
|
@@ -51,6 +80,14 @@ declare class MCPConfiguration extends MastraBase {
|
|
|
51
80
|
export { MCPConfiguration }
|
|
52
81
|
export { MCPConfiguration as MCPConfiguration_alias_1 }
|
|
53
82
|
|
|
83
|
+
declare interface MCPConfigurationOptions {
|
|
84
|
+
id?: string;
|
|
85
|
+
servers: Record<string, MastraMCPServerDefinition>;
|
|
86
|
+
timeout?: number;
|
|
87
|
+
}
|
|
88
|
+
export { MCPConfigurationOptions }
|
|
89
|
+
export { MCPConfigurationOptions as MCPConfigurationOptions_alias_1 }
|
|
90
|
+
|
|
54
91
|
export declare const server: Server<{
|
|
55
92
|
method: string;
|
|
56
93
|
params?: {
|
|
@@ -101,11 +138,6 @@ export declare const server_alias_1: Server<{
|
|
|
101
138
|
|
|
102
139
|
declare type SSEClientParameters = {
|
|
103
140
|
url: URL;
|
|
104
|
-
timeout?: number;
|
|
105
141
|
} & SSEClientTransportOptions;
|
|
106
142
|
|
|
107
|
-
declare type StdioServerParametersWithTimeout = StdioServerParameters & {
|
|
108
|
-
timeout?: number;
|
|
109
|
-
};
|
|
110
|
-
|
|
111
143
|
export { }
|