Auto-unwrap FastMCP results
What variant of Codex are you using?
App, CLI
What feature would you like to see?
FastMCP servers will auto-wrap all tool responses in a {"result": ...} JSON structure in order to comply with the MCP spec when returning primitive values (see: https://fastmcp.wiki/en/clients/tools#primitive-type-unwrapping). FastMCP assumes that the MCP client will auto-unwrap these.
Codex does not currently unwrap FastMCP results. I tested both Gemini CLI and the VSCode GitHub Copilot extension, and they both seem to unwrap them.
I think this would be a nice feature to have. With the current behavior, any FastMCP primitive strings are kept JSON-encoded, meaning newlines are escaped. For example:
{"result": "example1\nexample2"}
instead of
example1
example2
I've noticed that with strings with lots of newlines, model performance seems to be slightly worse when in this escaped form rather than as a raw string.
In order to avoid tampering with non-FastMCP servers that happen to return JSON of the form {"result": ...}, FastMCP seems to set "x-fastmcp-wrap-result": true in the schema which can be used to detect FastMCP servers.
The only potential downside I can think of this feature is that it increases risk of prompt injection, so (maybe) this could be an opt-in feature in the config.toml.
Additional information
_No response_
2 Comments
Hi @lunbun and team,
Thanks for the detailed issue and for pointing out the "x-fastmcp-wrap-result": true flag. That makes implementing a precise, safe unwrap much easier without risking unintended behavior for non-FastMCP servers!
You are completely right about the model performance degradation. Heavily escaped newlines (\n) instead of raw line breaks disrupt the LLM's spatial reasoning and attention mechanisms over the context window.
Proposed Implementation Plan:
Schema Inspection: During the MCP tool registration/discovery phase, we store whether a tool's schema contains "x-fastmcp-wrap-result": true.
Interceptor Logic: After tool execution, if this flag was present for the called tool, the MCP client middleware will parse the JSON payload and extract the raw string value from the "result" key.
Configuration & Security: To address the valid prompt injection concern, we can add a setting in config.toml (e.g., mcp.auto_unwrap_fastmcp). We can discuss if this should default to false for strict security, or true to match the parity with Gemini CLI and VSCode Copilot.
I would love to tackle this and contribute to the Codex project! If this architectural approach aligns with the maintainers' vision, please let me know and I can open a PR with the implementation and the necessary test coverage.
This feature hasn't received enough upvotes, so closing.