@mastra/openai 1.0.1-alpha.9 → 1.0.4-alpha.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.
@@ -0,0 +1,425 @@
1
+ # SDK agents
2
+
3
+ SDK agents let you use other agent SDK frameworks inside Mastra. Use them to register SDK-backed agents in a Mastra project while the provider SDK keeps its own runtime, tools, permissions, and agent loop.
4
+
5
+ ## When to use SDK agents
6
+
7
+ - A vendor SDK already owns the agent loop, tools, permissions, or local runtime.
8
+ - You want to register that SDK-backed agent in a Mastra project.
9
+ - You need Mastra-compatible `generate()` and `stream()` outputs.
10
+ - You want usage, cost, and tool activity from the SDK run to appear in Mastra observability.
11
+
12
+ ## Supported SDK agents
13
+
14
+ - [Claude Agent SDK](#claude-agent-sdk): Use `@mastra/claude` to register a Claude SDK agent and call it with Mastra `generate()` and `stream()`.
15
+ - [Cursor Agent SDK](#cursor-agent-sdk): Use `@mastra/cursor` to register a Cursor SDK agent and call it with Mastra `generate()` and `stream()`.
16
+ - [OpenAI Agents SDK](#openai-agents-sdk): Use `@mastra/openai` to register an OpenAI SDK agent and call it with Mastra `generate()` and `stream()`.
17
+
18
+ ## Claude Agent SDK
19
+
20
+ Use `@mastra/claude` for Claude Code runtime configuration, permissions, tools, and agent-loop behavior.
21
+
22
+ ### Install Claude packages
23
+
24
+ Install the Mastra package and the Claude Agent SDK peer dependency:
25
+
26
+ **npm**:
27
+
28
+ ```bash
29
+ npm install @mastra/claude @anthropic-ai/claude-agent-sdk
30
+ ```
31
+
32
+ **pnpm**:
33
+
34
+ ```bash
35
+ pnpm add @mastra/claude @anthropic-ai/claude-agent-sdk
36
+ ```
37
+
38
+ **Yarn**:
39
+
40
+ ```bash
41
+ yarn add @mastra/claude @anthropic-ai/claude-agent-sdk
42
+ ```
43
+
44
+ **Bun**:
45
+
46
+ ```bash
47
+ bun add @mastra/claude @anthropic-ai/claude-agent-sdk
48
+ ```
49
+
50
+ Set the Claude SDK credential:
51
+
52
+ ```bash
53
+ export ANTHROPIC_API_KEY="..."
54
+ ```
55
+
56
+ ### Create a Claude SDK agent
57
+
58
+ Configure Claude Agent SDK through `sdkOptions`.
59
+
60
+ ```typescript
61
+ import { ClaudeSDKAgent } from '@mastra/claude'
62
+
63
+ export const claudeSDKAgent = new ClaudeSDKAgent({
64
+ id: 'claude-sdk-agent',
65
+ name: 'Claude SDK Agent',
66
+ description: 'Use Claude Agent SDK through Mastra.',
67
+ sdkOptions: {
68
+ model: 'claude-sonnet-4-6',
69
+ cwd: process.cwd(),
70
+ },
71
+ })
72
+ ```
73
+
74
+ ### Add Claude SDK tools
75
+
76
+ Claude Agent SDK tools are provided through Claude SDK Model Context Protocol (MCP) servers. Create the server with the Claude SDK, then pass it through `sdkOptions.mcpServers`.
77
+
78
+ ```typescript
79
+ import { createSdkMcpServer } from '@anthropic-ai/claude-agent-sdk'
80
+ import { ClaudeSDKAgent } from '@mastra/claude'
81
+ import { getTemperature } from '../tools/get-temperature'
82
+
83
+ const weatherServer = createSdkMcpServer({
84
+ name: 'weather',
85
+ version: '1.0.0',
86
+ tools: [getTemperature],
87
+ })
88
+
89
+ export const claudeSDKAgent = new ClaudeSDKAgent({
90
+ id: 'claude-sdk-agent',
91
+ name: 'Claude SDK Agent',
92
+ description: 'Use Claude Agent SDK through Mastra.',
93
+ sdkOptions: {
94
+ model: 'claude-sonnet-4-6',
95
+ cwd: process.cwd(),
96
+ mcpServers: {
97
+ weather: weatherServer,
98
+ },
99
+ allowedTools: ['mcp__weather__get_temperature'],
100
+ },
101
+ })
102
+ ```
103
+
104
+ The `allowedTools` value uses Claude Agent SDK MCP tool naming: `mcp__<server name>__<tool name>`.
105
+
106
+ ## Cursor Agent SDK
107
+
108
+ Use `@mastra/cursor` to register a Cursor SDK agent in Mastra while keeping Cursor-specific setup in Cursor SDK options.
109
+
110
+ ### Install Cursor packages
111
+
112
+ Install the Mastra package and the Cursor SDK peer dependency:
113
+
114
+ **npm**:
115
+
116
+ ```bash
117
+ npm install @mastra/cursor @cursor/sdk
118
+ ```
119
+
120
+ **pnpm**:
121
+
122
+ ```bash
123
+ pnpm add @mastra/cursor @cursor/sdk
124
+ ```
125
+
126
+ **Yarn**:
127
+
128
+ ```bash
129
+ yarn add @mastra/cursor @cursor/sdk
130
+ ```
131
+
132
+ **Bun**:
133
+
134
+ ```bash
135
+ bun add @mastra/cursor @cursor/sdk
136
+ ```
137
+
138
+ Set the Cursor SDK credential:
139
+
140
+ ```bash
141
+ export CURSOR_API_KEY="..."
142
+ ```
143
+
144
+ ### Create a Cursor SDK agent
145
+
146
+ Configure Cursor Agent SDK through `sdkOptions`.
147
+
148
+ ```typescript
149
+ import { CursorSDKAgent } from '@mastra/cursor'
150
+
151
+ export const cursorSDKAgent = new CursorSDKAgent({
152
+ id: 'cursor-sdk-agent',
153
+ name: 'Cursor SDK Agent',
154
+ description: 'Use Cursor Agent SDK through Mastra.',
155
+ sdkOptions: {
156
+ apiKey: process.env.CURSOR_API_KEY,
157
+ model: {
158
+ id: 'gpt-5',
159
+ },
160
+ local: {
161
+ cwd: process.cwd(),
162
+ },
163
+ },
164
+ })
165
+ ```
166
+
167
+ Cursor local agents require an explicit model. Set it in `sdkOptions.model`.
168
+
169
+ ### Use an existing Cursor SDK agent
170
+
171
+ If your app already creates a Cursor SDK agent, pass that agent to `CursorSDKAgent` instead:
172
+
173
+ ```typescript
174
+ import { Agent as CursorAgent } from '@cursor/sdk'
175
+ import { CursorSDKAgent } from '@mastra/cursor'
176
+
177
+ const cursorAgent = CursorAgent.create({
178
+ apiKey: process.env.CURSOR_API_KEY,
179
+ model: {
180
+ id: 'gpt-5',
181
+ },
182
+ local: {
183
+ cwd: process.cwd(),
184
+ },
185
+ })
186
+
187
+ export const cursorSDKAgent = new CursorSDKAgent({
188
+ id: 'cursor-sdk-agent',
189
+ name: 'Cursor SDK Agent',
190
+ description: 'Use Cursor Agent SDK through Mastra.',
191
+ agent: cursorAgent,
192
+ })
193
+ ```
194
+
195
+ ### Add Cursor SDK tools
196
+
197
+ Cursor Agent SDK tools are configured with Cursor SDK options. Pass Model Context Protocol (MCP) servers through `sdkOptions.mcpServers`:
198
+
199
+ ```typescript
200
+ import { CursorSDKAgent } from '@mastra/cursor'
201
+ import { mcpServers } from '../mcp/cursor'
202
+
203
+ export const cursorSDKAgent = new CursorSDKAgent({
204
+ id: 'cursor-sdk-agent',
205
+ name: 'Cursor SDK Agent',
206
+ description: 'Use Cursor Agent SDK through Mastra.',
207
+ sdkOptions: {
208
+ apiKey: process.env.CURSOR_API_KEY,
209
+ model: {
210
+ id: 'gpt-5',
211
+ },
212
+ local: {
213
+ cwd: process.cwd(),
214
+ },
215
+ mcpServers,
216
+ },
217
+ })
218
+ ```
219
+
220
+ ## OpenAI Agents SDK
221
+
222
+ Use `@mastra/openai` to register an OpenAI Agents SDK agent in Mastra while keeping OpenAI-specific agent settings in OpenAI SDK options.
223
+
224
+ ### Install OpenAI packages
225
+
226
+ Install the Mastra package and the OpenAI Agents SDK peer dependency:
227
+
228
+ **npm**:
229
+
230
+ ```bash
231
+ npm install @mastra/openai @openai/agents zod
232
+ ```
233
+
234
+ **pnpm**:
235
+
236
+ ```bash
237
+ pnpm add @mastra/openai @openai/agents zod
238
+ ```
239
+
240
+ **Yarn**:
241
+
242
+ ```bash
243
+ yarn add @mastra/openai @openai/agents zod
244
+ ```
245
+
246
+ **Bun**:
247
+
248
+ ```bash
249
+ bun add @mastra/openai @openai/agents zod
250
+ ```
251
+
252
+ Set the OpenAI SDK credential:
253
+
254
+ ```bash
255
+ export OPENAI_API_KEY="..."
256
+ ```
257
+
258
+ ### Create an OpenAI SDK agent
259
+
260
+ Configure OpenAI Agents SDK through `sdkOptions`. `OpenAISDKAgent` creates the OpenAI SDK agent on first use.
261
+
262
+ ```typescript
263
+ import { OpenAISDKAgent } from '@mastra/openai'
264
+
265
+ export const openaiSDKAgent = new OpenAISDKAgent({
266
+ id: 'openai-sdk-agent',
267
+ name: 'OpenAI SDK Agent',
268
+ description: 'Use OpenAI Agents SDK through Mastra.',
269
+ sdkOptions: {
270
+ name: 'Repository assistant',
271
+ instructions: 'Answer clearly and cite the relevant files.',
272
+ model: 'gpt-5',
273
+ },
274
+ })
275
+ ```
276
+
277
+ ### Use an existing OpenAI SDK agent
278
+
279
+ If your app already creates an OpenAI SDK agent, pass that agent to `OpenAISDKAgent` instead:
280
+
281
+ ```typescript
282
+ import { Agent as OpenAIAgent } from '@openai/agents'
283
+ import { OpenAISDKAgent } from '@mastra/openai'
284
+
285
+ const sdkAgent = new OpenAIAgent({
286
+ name: 'Repository assistant',
287
+ instructions: 'Answer clearly and cite the relevant files.',
288
+ model: 'gpt-5',
289
+ })
290
+
291
+ export const openaiSDKAgent = new OpenAISDKAgent({
292
+ id: 'openai-sdk-agent',
293
+ name: 'OpenAI SDK Agent',
294
+ description: 'Use OpenAI Agents SDK through Mastra.',
295
+ agent: sdkAgent,
296
+ })
297
+ ```
298
+
299
+ ### Add OpenAI SDK tools
300
+
301
+ OpenAI Agents SDK tools are configured with OpenAI SDK options. Create tools with the OpenAI SDK, then pass them through `sdkOptions.tools`.
302
+
303
+ ```typescript
304
+ import { tool } from '@openai/agents'
305
+ import { OpenAISDKAgent } from '@mastra/openai'
306
+ import { z } from 'zod'
307
+
308
+ const getTemperature = tool({
309
+ name: 'get_temperature',
310
+ description: 'Get the current temperature for a city.',
311
+ parameters: z.object({
312
+ city: z.string(),
313
+ }),
314
+ execute: async ({ city }) => {
315
+ return `${city}: 27 C`
316
+ },
317
+ })
318
+
319
+ export const openaiSDKAgent = new OpenAISDKAgent({
320
+ id: 'openai-sdk-agent',
321
+ name: 'OpenAI SDK Agent',
322
+ description: 'Use OpenAI Agents SDK through Mastra.',
323
+ sdkOptions: {
324
+ name: 'Weather assistant',
325
+ model: 'gpt-5',
326
+ tools: [getTemperature],
327
+ },
328
+ })
329
+ ```
330
+
331
+ ## Register SDK agents
332
+
333
+ Register SDK agents in the Mastra instance like other agents:
334
+
335
+ ```typescript
336
+ import { Mastra } from '@mastra/core'
337
+ import { claudeSDKAgent } from './agents/claude-sdk-agent'
338
+ import { cursorSDKAgent } from './agents/cursor-sdk-agent'
339
+ import { openaiSDKAgent } from './agents/openai-sdk-agent'
340
+
341
+ export const mastra = new Mastra({
342
+ agents: {
343
+ claudeSDKAgent,
344
+ cursorSDKAgent,
345
+ openaiSDKAgent,
346
+ },
347
+ })
348
+ ```
349
+
350
+ After registration, call them with `mastra.getAgentById()`:
351
+
352
+ ```typescript
353
+ import { mastra } from './index'
354
+
355
+ const agent = mastra.getAgentById('cursor-sdk-agent')
356
+ const stream = await agent.stream('Inspect this project and describe the test setup.')
357
+
358
+ for await (const chunk of stream.textStream) {
359
+ process.stdout.write(chunk)
360
+ }
361
+ ```
362
+
363
+ ## Resume SDK runs
364
+
365
+ SDK agents support Mastra `resumeGenerate()` and `resumeStream()` with provider-native resume data. Pass the message to continue with and the resume identifier used by the underlying SDK.
366
+
367
+ Claude SDK agents can resume a known session with `sessionId`:
368
+
369
+ ```typescript
370
+ const result = await claudeSDKAgent.resumeGenerate({
371
+ message: 'Continue the previous task.',
372
+ sessionId: 'claude-session-id',
373
+ })
374
+
375
+ console.log(result.text)
376
+ ```
377
+
378
+ OpenAI SDK agents can resume with a previous response, conversation, or session:
379
+
380
+ ```typescript
381
+ const stream = await openaiSDKAgent.resumeStream({
382
+ message: 'Continue the previous task.',
383
+ previousResponseId: 'resp_123',
384
+ })
385
+
386
+ for await (const chunk of stream.textStream) {
387
+ process.stdout.write(chunk)
388
+ }
389
+ ```
390
+
391
+ Cursor SDK agents can continue with the wrapped SDK agent. If you need to resume a stored Cursor SDK agent by ID, pass `agentId` in `resumeData`.
392
+
393
+ ## Structured output
394
+
395
+ Claude and OpenAI SDK agents support Mastra `structuredOutput` through their provider-native structured output APIs. The validated value is available on `result.object`.
396
+
397
+ ```typescript
398
+ import { z } from 'zod'
399
+
400
+ const result = await openaiSDKAgent.generate<{ summary: string }>('Summarize this project.', {
401
+ structuredOutput: {
402
+ schema: z.object({
403
+ summary: z.string(),
404
+ }),
405
+ },
406
+ })
407
+
408
+ console.log(result.object.summary)
409
+ ```
410
+
411
+ Cursor SDK agents throw a clear error when `structuredOutput` is requested because the Cursor TypeScript SDK doesn't expose a schema-constrained output API.
412
+
413
+ ## Observability
414
+
415
+ SDK agents create Mastra agent and model spans for `generate()` and `stream()` calls. Mastra records SDK-provided usage, tool activity, and provider metadata when the vendor SDK exposes those events.
416
+
417
+ Claude SDK runs can include SDK-estimated cost from the Claude result message. Cursor SDK runs include token usage from Cursor interaction updates. OpenAI SDK runs include token usage from OpenAI run state.
418
+
419
+ For storage and dashboard setup, see [Observability](https://mastra.ai/docs/observability/overview).
420
+
421
+ ## Related
422
+
423
+ - [Agents overview](https://mastra.ai/docs/agents/overview)
424
+ - [Tools](https://mastra.ai/docs/agents/using-tools)
425
+ - [Observability](https://mastra.ai/docs/observability/overview)