@mastra/mcp 1.1.0 → 1.2.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/CHANGELOG.md +56 -0
- package/dist/client/actions/progress.d.ts +2 -3
- package/dist/client/actions/progress.d.ts.map +1 -1
- package/dist/client/actions/resource.d.ts +6 -2
- package/dist/client/actions/resource.d.ts.map +1 -1
- package/dist/client/client.d.ts +171 -175
- package/dist/client/client.d.ts.map +1 -1
- package/dist/client/configuration.d.ts +6 -2
- package/dist/client/configuration.d.ts.map +1 -1
- package/dist/client/types.d.ts +17 -0
- package/dist/client/types.d.ts.map +1 -1
- package/dist/docs/SKILL.md +3 -3
- package/dist/docs/assets/SOURCE_MAP.json +1 -1
- package/dist/docs/references/docs-mcp-overview.md +1 -1
- package/dist/docs/references/docs-mcp-publishing-mcp-server.md +4 -4
- package/dist/docs/references/reference-tools-mcp-client.md +13 -13
- package/dist/docs/references/reference-tools-mcp-server.md +27 -27
- package/dist/index.cjs +62 -89
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +63 -90
- package/dist/index.js.map +1 -1
- package/dist/server/server.d.ts +1 -0
- package/dist/server/server.d.ts.map +1 -1
- package/package.json +10 -11
- package/dist/__fixtures__/fire-crawl-complex-schema.d.ts +0 -5
- package/dist/__fixtures__/fire-crawl-complex-schema.d.ts.map +0 -1
- package/dist/__fixtures__/server-weather.d.ts +0 -2
- package/dist/__fixtures__/server-weather.d.ts.map +0 -1
- package/dist/__fixtures__/stock-price.d.ts +0 -26
- package/dist/__fixtures__/stock-price.d.ts.map +0 -1
- package/dist/__fixtures__/tools.d.ts +0 -12
- package/dist/__fixtures__/tools.d.ts.map +0 -1
- package/dist/__fixtures__/weather.d.ts +0 -4
- package/dist/__fixtures__/weather.d.ts.map +0 -1
package/dist/client/types.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import type { IOType } from 'node:child_process';
|
|
1
2
|
import type { RequestContext } from '@mastra/core/di';
|
|
2
3
|
import type { SSEClientTransportOptions } from '@modelcontextprotocol/sdk/client/sse.js';
|
|
3
4
|
import type { StreamableHTTPClientTransportOptions } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
|
|
@@ -137,6 +138,20 @@ export type StdioServerDefinition = BaseServerOptions & {
|
|
|
137
138
|
args?: string[];
|
|
138
139
|
/** Optional environment variables for the subprocess */
|
|
139
140
|
env?: Record<string, string>;
|
|
141
|
+
/**
|
|
142
|
+
* How to handle stderr of the child process. Matches the semantics of Node's `child_process.spawn`.
|
|
143
|
+
*
|
|
144
|
+
* - `"inherit"` (default): stderr is printed to the parent process's stderr
|
|
145
|
+
* - `"pipe"`: stderr is captured and available via `StdioClientTransport.stderr`
|
|
146
|
+
* - `"ignore"`: stderr is discarded
|
|
147
|
+
*/
|
|
148
|
+
stderr?: IOType;
|
|
149
|
+
/**
|
|
150
|
+
* The working directory to use when spawning the subprocess.
|
|
151
|
+
*
|
|
152
|
+
* If not specified, the current working directory will be inherited.
|
|
153
|
+
*/
|
|
154
|
+
cwd?: string;
|
|
140
155
|
url?: never;
|
|
141
156
|
requestInit?: never;
|
|
142
157
|
eventSourceInit?: never;
|
|
@@ -161,6 +176,8 @@ export type HttpServerDefinition = BaseServerOptions & {
|
|
|
161
176
|
command?: never;
|
|
162
177
|
args?: never;
|
|
163
178
|
env?: never;
|
|
179
|
+
stderr?: never;
|
|
180
|
+
cwd?: never;
|
|
164
181
|
/**
|
|
165
182
|
* Custom fetch implementation used for all network requests.
|
|
166
183
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/client/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,yCAAyC,CAAC;AACzF,OAAO,KAAK,EAAE,oCAAoC,EAAE,MAAM,oDAAoD,CAAC;AAE/G,Y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,oCAAoC,CAAC,aAAa,CAAC,CAAC;IAClE,+HAA+H;IAC/H,eAAe,CAAC,EAAE,yBAAyB,CAAC,iBAAiB,CAAC,CAAC;IAC/D,6FAA6F;IAC7F,YAAY,CAAC,EAAE,oCAAoC,CAAC,cAAc,CAAC,CAAC;IACpE,8DAA8D;IAC9D,mBAAmB,CAAC,EAAE,oCAAoC,CAAC,qBAAqB,CAAC,CAAC;IAClF,8CAA8C;IAC9C,SAAS,CAAC,EAAE,oCAAoC,CAAC,WAAW,CAAC,CAAC;IAC9D;;;OAGG;IACH,cAAc,CAAC,EAAE,MAAM,CAAC;CACzB,CAAC;AAEF;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsCG;AACH,MAAM,MAAM,yBAAyB,GAAG,qBAAqB,GAAG,oBAAoB,CAAC;AAErF;;;;GAIG;AACH,MAAM,MAAM,8BAA8B,GAAG;IAC3C,sCAAsC;IACtC,IAAI,EAAE,MAAM,CAAC;IACb,sCAAsC;IACtC,MAAM,EAAE,yBAAyB,CAAC;IAClC,8DAA8D;IAC9D,YAAY,CAAC,EAAE,kBAAkB,CAAC;IAClC,8BAA8B;IAC9B,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uCAAuC;IACvC,OAAO,CAAC,EAAE,MAAM,CAAC;CAClB,CAAC"}
|
package/dist/docs/SKILL.md
CHANGED
|
@@ -3,7 +3,7 @@ name: mastra-mcp
|
|
|
3
3
|
description: Documentation for @mastra/mcp. Use when working with @mastra/mcp APIs, configuration, or implementation.
|
|
4
4
|
metadata:
|
|
5
5
|
package: "@mastra/mcp"
|
|
6
|
-
version: "1.
|
|
6
|
+
version: "1.2.0"
|
|
7
7
|
---
|
|
8
8
|
|
|
9
9
|
## When to use
|
|
@@ -16,8 +16,8 @@ Read the individual reference documents for detailed explanations and code examp
|
|
|
16
16
|
|
|
17
17
|
### Docs
|
|
18
18
|
|
|
19
|
-
- [MCP
|
|
20
|
-
- [Publishing an MCP
|
|
19
|
+
- [MCP overview](references/docs-mcp-overview.md) - Learn about the Model Context Protocol (MCP), how to use third-party tools via MCPClient, connect to registries, and share your own tools using MCPServer.
|
|
20
|
+
- [Publishing an MCP server](references/docs-mcp-publishing-mcp-server.md) - Guide to setting up and building a Mastra MCP server using the stdio transport, and publishing it to NPM.
|
|
21
21
|
|
|
22
22
|
### Reference
|
|
23
23
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# MCP
|
|
1
|
+
# MCP overview
|
|
2
2
|
|
|
3
3
|
Mastra supports the [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction), an open standard for connecting AI agents to external tools and resources. It serves as a universal plugin system, enabling agents to call tools regardless of language or hosting environment.
|
|
4
4
|
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
# Publishing an MCP
|
|
1
|
+
# Publishing an MCP server
|
|
2
2
|
|
|
3
3
|
This example guides you through setting up a basic Mastra MCPServer using the stdio transport, building it, and preparing it for publishing to NPM.
|
|
4
4
|
|
|
@@ -10,7 +10,7 @@ Install the necessary packages:
|
|
|
10
10
|
pnpm add @mastra/mcp @mastra/core tsup
|
|
11
11
|
```
|
|
12
12
|
|
|
13
|
-
## Setting up an MCP
|
|
13
|
+
## Setting up an MCP server
|
|
14
14
|
|
|
15
15
|
1. Create a file for your stdio server, for example, `/src/mastra/stdio.ts`.
|
|
16
16
|
|
|
@@ -70,7 +70,7 @@ To make your MCP server available for others (or yourself) to use via `npx` or a
|
|
|
70
70
|
|
|
71
71
|
For more details on publishing packages, refer to the [NPM documentation](https://docs.npmjs.com/creating-and-publishing-scoped-public-packages).
|
|
72
72
|
|
|
73
|
-
## Using a published MCP
|
|
73
|
+
## Using a published MCP server
|
|
74
74
|
|
|
75
75
|
Once published, your MCP server can be used by an `MCPClient` by specifying the command to run your package. You can also use any other MCP client like Claude desktop, Cursor, or Windsurf.
|
|
76
76
|
|
|
@@ -92,4 +92,4 @@ const tools = await mcp.listTools()
|
|
|
92
92
|
const toolsets = await mcp.listToolsets()
|
|
93
93
|
```
|
|
94
94
|
|
|
95
|
-
Note: If you published without an organization scope, the `args` might
|
|
95
|
+
Note: If you published without an organization scope, the `args` might be `["-y", "your-package-name@latest"]`.
|
|
@@ -22,7 +22,7 @@ constructor({
|
|
|
22
22
|
|
|
23
23
|
**timeout** (`number`): Global timeout value in milliseconds for all servers unless overridden in individual server configs. (Default: `60000`)
|
|
24
24
|
|
|
25
|
-
### MastraMCPServerDefinition
|
|
25
|
+
### `MastraMCPServerDefinition`
|
|
26
26
|
|
|
27
27
|
Each server in the `servers` map is configured using the `MastraMCPServerDefinition` type. The transport type is detected based on the provided parameters:
|
|
28
28
|
|
|
@@ -55,7 +55,7 @@ Each server in the `servers` map is configured using the `MastraMCPServerDefinit
|
|
|
55
55
|
|
|
56
56
|
## Methods
|
|
57
57
|
|
|
58
|
-
### listTools()
|
|
58
|
+
### `listTools()`
|
|
59
59
|
|
|
60
60
|
Retrieves all tools from all configured servers, with tool names namespaced by their server name (in the format `serverName_toolName`) to prevent conflicts. Intended to be passed onto an Agent definition.
|
|
61
61
|
|
|
@@ -63,7 +63,7 @@ Retrieves all tools from all configured servers, with tool names namespaced by t
|
|
|
63
63
|
new Agent({ id: 'agent', tools: await mcp.listTools() })
|
|
64
64
|
```
|
|
65
65
|
|
|
66
|
-
### listToolsets()
|
|
66
|
+
### `listToolsets()`
|
|
67
67
|
|
|
68
68
|
Returns an object mapping namespaced tool names (in the format `serverName.toolName`) to their tool implementations. Intended to be passed dynamically into the generate or stream method.
|
|
69
69
|
|
|
@@ -73,7 +73,7 @@ const res = await agent.stream(prompt, {
|
|
|
73
73
|
})
|
|
74
74
|
```
|
|
75
75
|
|
|
76
|
-
### disconnect()
|
|
76
|
+
### `disconnect()`
|
|
77
77
|
|
|
78
78
|
Disconnects from all MCP servers and cleans up resources.
|
|
79
79
|
|
|
@@ -239,7 +239,7 @@ mcpClient.elicitation.onRequest('serverName', async request => {
|
|
|
239
239
|
|
|
240
240
|
Sets up a handler function that will be called when any connected MCP server sends an elicitation request. The handler receives the request and must return a response.
|
|
241
241
|
|
|
242
|
-
##### ElicitationHandler Function
|
|
242
|
+
##### `ElicitationHandler` Function
|
|
243
243
|
|
|
244
244
|
The handler function receives a request object with:
|
|
245
245
|
|
|
@@ -534,7 +534,7 @@ mcpClient.elicitation.onRequest('interactiveServer', async request => {
|
|
|
534
534
|
})
|
|
535
535
|
```
|
|
536
536
|
|
|
537
|
-
### Response
|
|
537
|
+
### Response types
|
|
538
538
|
|
|
539
539
|
Your elicitation handler must return one of three response types:
|
|
540
540
|
|
|
@@ -592,7 +592,7 @@ await mcpClient.elicitation.onRequest('interactiveServer', async request => {
|
|
|
592
592
|
})
|
|
593
593
|
```
|
|
594
594
|
|
|
595
|
-
### Best
|
|
595
|
+
### Best practices
|
|
596
596
|
|
|
597
597
|
- **Always handle elicitation**: Set up your handler before calling tools that might use elicitation
|
|
598
598
|
- **Validate input**: Check that required fields are provided
|
|
@@ -600,7 +600,7 @@ await mcpClient.elicitation.onRequest('interactiveServer', async request => {
|
|
|
600
600
|
- **Clear UI**: Make it obvious what information is being requested and why
|
|
601
601
|
- **Security**: Never auto-accept requests for sensitive information
|
|
602
602
|
|
|
603
|
-
## OAuth
|
|
603
|
+
## OAuth authentication
|
|
604
604
|
|
|
605
605
|
For connecting to MCP servers that require OAuth authentication per the [MCP Auth Specification](https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization), use the `MCPOAuthClientProvider`:
|
|
606
606
|
|
|
@@ -819,7 +819,7 @@ const response = await agent.stream('How is AAPL doing and what is the weather?'
|
|
|
819
819
|
})
|
|
820
820
|
```
|
|
821
821
|
|
|
822
|
-
## Instance
|
|
822
|
+
## Instance management
|
|
823
823
|
|
|
824
824
|
The `MCPClient` class includes built-in memory leak prevention for managing multiple instances:
|
|
825
825
|
|
|
@@ -863,7 +863,7 @@ const mcp4 = new MCPClient({
|
|
|
863
863
|
})
|
|
864
864
|
```
|
|
865
865
|
|
|
866
|
-
## Server
|
|
866
|
+
## Server lifecycle
|
|
867
867
|
|
|
868
868
|
MCPClient handles server connections gracefully:
|
|
869
869
|
|
|
@@ -871,7 +871,7 @@ MCPClient handles server connections gracefully:
|
|
|
871
871
|
2. Graceful server shutdown to prevent error messages during development
|
|
872
872
|
3. Proper cleanup of resources when disconnecting
|
|
873
873
|
|
|
874
|
-
## Using
|
|
874
|
+
## Using custom fetch for dynamic authentication
|
|
875
875
|
|
|
876
876
|
For HTTP servers, you can provide a custom `fetch` function to handle dynamic authentication, request interception, or other custom behavior. This is particularly useful when you need to refresh tokens on each request or forward user credentials from the incoming request to the MCP server.
|
|
877
877
|
|
|
@@ -910,7 +910,7 @@ await agent.generate('Hello!', {
|
|
|
910
910
|
})
|
|
911
911
|
```
|
|
912
912
|
|
|
913
|
-
## Using SSE
|
|
913
|
+
## Using SSE request headers
|
|
914
914
|
|
|
915
915
|
When using the legacy SSE MCP transport, you must configure both `requestInit` and `eventSourceInit` due to a bug in the MCP SDK. Alternatively, you can use a custom `fetch` function which will be automatically used for both POST requests and SSE connections:
|
|
916
916
|
|
|
@@ -959,7 +959,7 @@ const sseClientWithFetch = new MCPClient({
|
|
|
959
959
|
})
|
|
960
960
|
```
|
|
961
961
|
|
|
962
|
-
## Related
|
|
962
|
+
## Related information
|
|
963
963
|
|
|
964
964
|
- For creating MCP servers, see the [MCPServer documentation](https://mastra.ai/reference/tools/mcp-server).
|
|
965
965
|
- For more about the Model Context Protocol, see the [@modelcontextprotocol/sdk documentation](https://github.com/modelcontextprotocol/typescript-sdk).
|
|
@@ -83,7 +83,7 @@ The constructor accepts an `MCPServerConfig` object with the following propertie
|
|
|
83
83
|
|
|
84
84
|
**prompts** (`MCPServerPrompts`): An object defining how the server should handle MCP prompts. See Prompt Handling section for details.
|
|
85
85
|
|
|
86
|
-
## Exposing
|
|
86
|
+
## Exposing agents as tools
|
|
87
87
|
|
|
88
88
|
A powerful feature of `MCPServer` is its ability to automatically expose your Mastra Agents as callable tools. When you provide agents in the `agents` property of the configuration:
|
|
89
89
|
|
|
@@ -96,9 +96,9 @@ A powerful feature of `MCPServer` is its ability to automatically expose your Ma
|
|
|
96
96
|
- **Execution**: When this tool is called, it invokes the `generate()` method of the corresponding agent, passing the provided `query`.
|
|
97
97
|
- **Output**: The direct result from the agent's `generate()` method is returned as the output of the tool.
|
|
98
98
|
|
|
99
|
-
- **Name Collisions**: If an explicit tool defined in the `tools` configuration has the same name as an agent-derived tool (e.g., you have a tool named `ask_myAgentKey` and also an agent with the key `myAgentKey`), the _explicitly defined tool will take precedence_. The agent
|
|
99
|
+
- **Name Collisions**: If an explicit tool defined in the `tools` configuration has the same name as an agent-derived tool (e.g., you have a tool named `ask_myAgentKey` and also an agent with the key `myAgentKey`), the _explicitly defined tool will take precedence_. The agent won't be converted into a tool in this conflicting case, and a warning will be logged.
|
|
100
100
|
|
|
101
|
-
This makes it straightforward to allow MCP clients to interact with your agents using natural language queries,
|
|
101
|
+
This makes it straightforward to allow MCP clients to interact with your agents using natural language queries, like any other tool.
|
|
102
102
|
|
|
103
103
|
### Agent-to-Tool Conversion
|
|
104
104
|
|
|
@@ -164,7 +164,7 @@ const fetchUserData = createTool({
|
|
|
164
164
|
|
|
165
165
|
These are the functions you can call on an `MCPServer` instance to control its behavior and get information.
|
|
166
166
|
|
|
167
|
-
### startStdio()
|
|
167
|
+
### `startStdio()`
|
|
168
168
|
|
|
169
169
|
Use this method to start the server so it communicates using standard input and output (stdio). This is typical when running the server as a command-line program.
|
|
170
170
|
|
|
@@ -186,7 +186,7 @@ const server = new MCPServer({
|
|
|
186
186
|
await server.startStdio()
|
|
187
187
|
```
|
|
188
188
|
|
|
189
|
-
### startSSE()
|
|
189
|
+
### `startSSE()`
|
|
190
190
|
|
|
191
191
|
This method helps you integrate the MCP server with an existing web server to use Server-Sent Events (SSE) for communication. You'll call this from your web server's code when it receives a request for the SSE or message paths.
|
|
192
192
|
|
|
@@ -238,7 +238,7 @@ Here are the details for the values needed by the `startSSE` method:
|
|
|
238
238
|
|
|
239
239
|
**res** (`any`): The response object from your web server, used to send data back.
|
|
240
240
|
|
|
241
|
-
### startHonoSSE()
|
|
241
|
+
### `startHonoSSE()`
|
|
242
242
|
|
|
243
243
|
This method helps you integrate the MCP server with an existing web server to use Server-Sent Events (SSE) for communication. You'll call this from your web server's code when it receives a request for the SSE or message paths.
|
|
244
244
|
|
|
@@ -290,7 +290,7 @@ Here are the details for the values needed by the `startHonoSSE` method:
|
|
|
290
290
|
|
|
291
291
|
**res** (`any`): The response object from your web server, used to send data back.
|
|
292
292
|
|
|
293
|
-
### startHTTP()
|
|
293
|
+
### `startHTTP()`
|
|
294
294
|
|
|
295
295
|
This method helps you integrate the MCP server with an existing web server to use streamable HTTP for communication. You'll call this from your web server's code when it receives HTTP requests.
|
|
296
296
|
|
|
@@ -391,7 +391,7 @@ serve(async req => {
|
|
|
391
391
|
>
|
|
392
392
|
> The serverless mode disables session management and creates fresh server instances per request, which is necessary for stateless environments where memory doesn't persist between invocations.
|
|
393
393
|
>
|
|
394
|
-
> **Note:** The following MCP features require session state or persistent connections and
|
|
394
|
+
> **Note:** The following MCP features require session state or persistent connections and **won't work** in serverless mode:
|
|
395
395
|
>
|
|
396
396
|
> - **Elicitation** - Interactive user input requests during tool execution require session management to route responses back to the correct client
|
|
397
397
|
> - **Resource subscriptions** - `resources/subscribe` and `resources/unsubscribe` need persistent connections to maintain subscription state
|
|
@@ -424,7 +424,7 @@ The `StreamableHTTPServerTransportOptions` object allows you to customize the be
|
|
|
424
424
|
|
|
425
425
|
**eventStore** (`EventStore`): An event store for message resumability. Providing this enables clients to reconnect and resume message streams.
|
|
426
426
|
|
|
427
|
-
### close()
|
|
427
|
+
### `close()`
|
|
428
428
|
|
|
429
429
|
This method closes the server and releases all resources.
|
|
430
430
|
|
|
@@ -432,7 +432,7 @@ This method closes the server and releases all resources.
|
|
|
432
432
|
async close(): Promise<void>
|
|
433
433
|
```
|
|
434
434
|
|
|
435
|
-
### getServerInfo()
|
|
435
|
+
### `getServerInfo()`
|
|
436
436
|
|
|
437
437
|
This method gives you a look at the server's basic information.
|
|
438
438
|
|
|
@@ -440,7 +440,7 @@ This method gives you a look at the server's basic information.
|
|
|
440
440
|
getServerInfo(): ServerInfo
|
|
441
441
|
```
|
|
442
442
|
|
|
443
|
-
### getServerDetail()
|
|
443
|
+
### `getServerDetail()`
|
|
444
444
|
|
|
445
445
|
This method gives you a detailed look at the server's information.
|
|
446
446
|
|
|
@@ -448,7 +448,7 @@ This method gives you a detailed look at the server's information.
|
|
|
448
448
|
getServerDetail(): ServerDetail
|
|
449
449
|
```
|
|
450
450
|
|
|
451
|
-
### getToolListInfo()
|
|
451
|
+
### `getToolListInfo()`
|
|
452
452
|
|
|
453
453
|
This method gives you a look at the tools that were set up when you created the server. It's a read-only list, useful for debugging purposes.
|
|
454
454
|
|
|
@@ -456,7 +456,7 @@ This method gives you a look at the tools that were set up when you created the
|
|
|
456
456
|
getToolListInfo(): ToolListInfo
|
|
457
457
|
```
|
|
458
458
|
|
|
459
|
-
### getToolInfo()
|
|
459
|
+
### `getToolInfo()`
|
|
460
460
|
|
|
461
461
|
This method gives you detailed information about a specific tool.
|
|
462
462
|
|
|
@@ -464,7 +464,7 @@ This method gives you detailed information about a specific tool.
|
|
|
464
464
|
getToolInfo(toolName: string): ToolInfo
|
|
465
465
|
```
|
|
466
466
|
|
|
467
|
-
### executeTool()
|
|
467
|
+
### `executeTool()`
|
|
468
468
|
|
|
469
469
|
This method executes a specific tool and returns the result.
|
|
470
470
|
|
|
@@ -472,7 +472,7 @@ This method executes a specific tool and returns the result.
|
|
|
472
472
|
executeTool(toolName: string, input: any): Promise<any>
|
|
473
473
|
```
|
|
474
474
|
|
|
475
|
-
### getStdioTransport()
|
|
475
|
+
### `getStdioTransport()`
|
|
476
476
|
|
|
477
477
|
If you started the server with `startStdio()`, you can use this to get the object that manages the stdio communication. This is mostly for checking things internally or for testing.
|
|
478
478
|
|
|
@@ -480,7 +480,7 @@ If you started the server with `startStdio()`, you can use this to get the objec
|
|
|
480
480
|
getStdioTransport(): StdioServerTransport | undefined
|
|
481
481
|
```
|
|
482
482
|
|
|
483
|
-
### getSseTransport()
|
|
483
|
+
### `getSseTransport()`
|
|
484
484
|
|
|
485
485
|
If you started the server with `startSSE()`, you can use this to get the object that manages the SSE communication. Like `getStdioTransport`, this is mainly for internal checks or testing.
|
|
486
486
|
|
|
@@ -488,7 +488,7 @@ If you started the server with `startSSE()`, you can use this to get the object
|
|
|
488
488
|
getSseTransport(): SSEServerTransport | undefined
|
|
489
489
|
```
|
|
490
490
|
|
|
491
|
-
### getSseHonoTransport()
|
|
491
|
+
### `getSseHonoTransport()`
|
|
492
492
|
|
|
493
493
|
If you started the server with `startHonoSSE()`, you can use this to get the object that manages the SSE communication. Like `getSseTransport`, this is mainly for internal checks or testing.
|
|
494
494
|
|
|
@@ -496,7 +496,7 @@ If you started the server with `startHonoSSE()`, you can use this to get the obj
|
|
|
496
496
|
getSseHonoTransport(): SSETransport | undefined
|
|
497
497
|
```
|
|
498
498
|
|
|
499
|
-
### getStreamableHTTPTransport()
|
|
499
|
+
### `getStreamableHTTPTransport()`
|
|
500
500
|
|
|
501
501
|
If you started the server with `startHTTP()`, you can use this to get the object that manages the HTTP communication. Like `getSseTransport`, this is mainly for internal checks or testing.
|
|
502
502
|
|
|
@@ -504,7 +504,7 @@ If you started the server with `startHTTP()`, you can use this to get the object
|
|
|
504
504
|
getStreamableHTTPTransport(): StreamableHTTPServerTransport | undefined
|
|
505
505
|
```
|
|
506
506
|
|
|
507
|
-
### tools()
|
|
507
|
+
### `tools()`
|
|
508
508
|
|
|
509
509
|
Executes a specific tool provided by this MCP server.
|
|
510
510
|
|
|
@@ -522,7 +522,7 @@ async executeTool(
|
|
|
522
522
|
|
|
523
523
|
**executionContext** (`object`): Optional context for the tool execution, like messages or a toolCallId.
|
|
524
524
|
|
|
525
|
-
## Resource
|
|
525
|
+
## Resource handling
|
|
526
526
|
|
|
527
527
|
### What are MCP Resources?
|
|
528
528
|
|
|
@@ -649,7 +649,7 @@ Example:
|
|
|
649
649
|
await serverWithResources.resources.notifyListChanged()
|
|
650
650
|
```
|
|
651
651
|
|
|
652
|
-
## Prompt
|
|
652
|
+
## Prompt handling
|
|
653
653
|
|
|
654
654
|
### What are MCP Prompts?
|
|
655
655
|
|
|
@@ -758,7 +758,7 @@ Call this method when the overall list of available prompts has changed (e.g., a
|
|
|
758
758
|
await serverWithPrompts.prompts.notifyListChanged()
|
|
759
759
|
```
|
|
760
760
|
|
|
761
|
-
### Best
|
|
761
|
+
### Best practices for Prompt Handling
|
|
762
762
|
|
|
763
763
|
- Use clear, descriptive prompt names and descriptions.
|
|
764
764
|
- Validate all required arguments in `getPromptMessages`.
|
|
@@ -778,7 +778,7 @@ The example at the beginning of this page also demonstrates how to instantiate `
|
|
|
778
778
|
|
|
779
779
|
## Elicitation
|
|
780
780
|
|
|
781
|
-
### What
|
|
781
|
+
### What's Elicitation?
|
|
782
782
|
|
|
783
783
|
Elicitation is a feature in the Model Context Protocol (MCP) that allows servers to request structured information from users. This enables interactive workflows where servers can collect additional data dynamically.
|
|
784
784
|
|
|
@@ -982,7 +982,7 @@ type ElicitResult = {
|
|
|
982
982
|
}
|
|
983
983
|
```
|
|
984
984
|
|
|
985
|
-
## OAuth
|
|
985
|
+
## OAuth protection
|
|
986
986
|
|
|
987
987
|
To protect your MCP server with OAuth authentication per the [MCP Auth Specification](https://modelcontextprotocol.io/specification/2025-06-18/basic/authorization), use the `createOAuthMiddleware` function:
|
|
988
988
|
|
|
@@ -1085,11 +1085,11 @@ const customMiddleware = createOAuthMiddleware({
|
|
|
1085
1085
|
|
|
1086
1086
|
**mcpPath** (`string`): Path where the MCP endpoint is served. Only requests to this path require authentication. (Default: `'/mcp'`)
|
|
1087
1087
|
|
|
1088
|
-
## Authentication
|
|
1088
|
+
## Authentication context
|
|
1089
1089
|
|
|
1090
1090
|
Tools can access request metadata via `context.mcp.extra` when using HTTP-based transports. This allows you to pass authentication info, user context, or any custom data from your HTTP middleware to your MCP tools.
|
|
1091
1091
|
|
|
1092
|
-
### How
|
|
1092
|
+
### How it works
|
|
1093
1093
|
|
|
1094
1094
|
Whatever you set on `req.auth` in your HTTP middleware becomes available as `context.mcp.extra.authInfo` in your tools:
|
|
1095
1095
|
|
|
@@ -1265,7 +1265,7 @@ app.all('/mcp', async (req, res) => {
|
|
|
1265
1265
|
app.listen(3000)
|
|
1266
1266
|
```
|
|
1267
1267
|
|
|
1268
|
-
## Related
|
|
1268
|
+
## Related information
|
|
1269
1269
|
|
|
1270
1270
|
- For connecting to MCP servers in Mastra, see the [MCPClient documentation](https://mastra.ai/reference/tools/mcp-client).
|
|
1271
1271
|
- For more about the Model Context Protocol, see the [@modelcontextprotocol/sdk documentation](https://github.com/modelcontextprotocol/typescript-sdk).
|