Skip to content

Conversation

SkinnnyJay
Copy link
Contributor

@SkinnnyJay SkinnnyJay commented Sep 14, 2025

Summary

Motivation

Previously, MCP server callTool only returned result.content. If a tool returned only structuredOutput, agents saw an empty array. This PR brings parity with the Python SDK by exposing structuredContent and providing a toggle to control inclusion in tool outputs.

Changes

  • API: MCPServer#callTool(...) now returns CallToolResult (full object: { content, structuredContent? }).
  • Option: useStructuredContent?: boolean added to all MCP server options and stored on base classes. Default false to avoid duplicate data when servers include structured output inside content.
  • Composition in mcpToFunctionTool:
    • Default (flag off):
      • Single content item → return that item object
      • Multiple content items → return array of items
    • With useStructuredContent true:
      • Single content item → return [contentItem, structuredContent]
      • Multiple content items → append structuredContent to the array
      • No content → return structuredContent object
  • No stringification: We keep JS outputs as objects/arrays to avoid forcing downstream JSON.parse. This differs from Python’s string concatenation but preserves capability parity and improves ergonomics/perf in JS.

Before/After

Before

// Returned only content[]
const content = await server.callTool('tool', args); // [] when tool only returned structuredOutput

After

// Returns full object
const result = await server.callTool('tool', args);
// result: { content: [...], structuredContent?: any }
// Default (useStructuredContent: false)
// Single content → object; multiple → array
// With useStructuredContent: true
const server = new MCPServerStdio({ command: 'my-mcp', useStructuredContent: true });
// Single content + structured → [contentItem, structuredContent]
// Only structured → structuredContent object

Tests

  • Added packages/agents-core/test/mcpStructuredContent.test.ts:
    • Default off: single content returns the item object.
    • With flag true:
      • Single content + structured → array with structured appended.
      • No content + structured → structured object.
      • Multiple content + structured → array with structured appended.
  • Updated mcpToolFilter.integration.test.ts stub to return { content: [...] }.
  • All tests pass: 393 tests across 62 files.

Docs

  • Added a section to packages/agents-core/README.md documenting useStructuredContent, the new callTool return type, and rationale for not stringifying.

Breaking Impact

  • Minimal breaking change: Only affects advanced users who directly call MCPServer#callTool and expect an array return type.
  • Behavioral outputs for mcpToFunctionTool remain backward-compatible by default (objects/arrays), with optional structuredContent inclusion when enabled.

Checklist

Copy link

changeset-bot bot commented Sep 14, 2025

🦋 Changeset detected

Latest commit: b075226

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 4 packages
Name Type
@openai/agents-core Patch
@openai/agents-openai Patch
@openai/agents-realtime Patch
@openai/agents Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@SkinnnyJay SkinnnyJay changed the title fix(agents-core/mcp)#468: support structuredContent via useStructuredContent; return full CallToolResult feat(mcp): support structuredContent via useStructuredContent; return full CallToolResult Sep 14, 2025
@seratch
Copy link
Member

seratch commented Sep 15, 2025

@codex review this

@seratch seratch added enhancement New feature or request package:agents-core labels Sep 15, 2025
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Codex Review: Here are some suggestions.

Reply with @codex fix comments to fix any unresolved comments.

About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you open a pull request for review, mark a draft as ready, or comment "@codex review". If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex fix this CI failure" or "@codex address that feedback".

Copy link
Member

@seratch seratch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for quickly checking my comments! Once they're addressed, we will check this PR then.

@seratch seratch marked this pull request as draft September 25, 2025 06:29
@SkinnnyJay
Copy link
Contributor Author

@codex review this

SkinnnyJay pushed a commit to SkinnnyJay/openai-agents-js that referenced this pull request Sep 26, 2025
…ompatibility

- Change changeset from minor to patch per @seratch feedback
- Fix hardcoded 'Error running tool.' fallback that broke backward compatibility
- Now returns result.content (empty array) when no content, preserving original behavior
- Addresses feedback from openai#471 (review)
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

@SkinnnyJay
Copy link
Contributor Author

@codex review this

@SkinnnyJay SkinnnyJay requested a review from seratch September 26, 2025 07:34
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

@SkinnnyJay SkinnnyJay force-pushed the bug-468 branch 2 times, most recently from 94da658 to 8c83f3f Compare September 26, 2025 08:10
…issues

- Add useStructuredContent option to MCP servers (stdio/streamable-http/SSE)
- Return full CallToolResult from callTool() instead of just content array
- Enable structured content support with JSON string returns for Python SDK consistency
- Fix TypeScript compilation errors (TS2352) in MCP server shims
- Add comprehensive tests for structuredContent feature
- Update test mocks to use proper CallToolResult interface
- Add changeset documenting the breaking change

Breaking Change: MCPServer.callTool() now returns CallToolResult (full object)
instead of CallToolResultContent (content array only). This enables access to
optional structuredContent while maintaining backward compatibility through
the content property.

Resolves compilation errors and adds structured content capabilities.
@SkinnnyJay SkinnnyJay marked this pull request as ready for review September 26, 2025 15:55
@seratch seratch merged commit be686e9 into openai:main Sep 26, 2025
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support structureOutput from MCP server tools
2 participants