Skip to content

feat(langchain): bind the Server's MCP tools non-strict on the Responses API - #3002

Open
jasper-yl wants to merge 3 commits into
oceanbase:masterfrom
jasper-yl:fix/langchain-responses-mcp-strict
Open

jasper-yl wants to merge 3 commits into
oceanbase:masterfrom
jasper-yl:fix/langchain-responses-mcp-strict

Conversation

@jasper-yl

@jasper-yl jasper-yl commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Which issue or RFC does this PR close?

Closes #2999.

Rationale for this change

Follow-up to #2996 / #2998 for the generic MCP-to-LangChain binding path. ChatOpenAI(use_responses_api=True).bind_tools(tools) uses strict=None by default and leaves strict out of every function tool. The Responses API applies strict mode to a function tool that omits the field, and a request that carries the full PowerContext MCP catalog (63 tools, 129 anyOf) then fails. Sending the tools with strict: false and unchanged schemas makes the request complete.

create_agent binds its tools itself, so a caller cannot pass strict there. Per the decision in #2999, this PR adds a supported entry point in powercontext-langchain for both paths, scoped to the tools the caller passes.

What changes are included in this PR?

  • powercontext_langchain.mcp_tool_definitions(tools): returns function tool definitions that set strict: false themselves and keep each input schema. It accepts MCP Tool objects (as list_tools() returns them) and LangChain BaseTools. model.bind_tools([*mcp_tool_definitions(mcp_tools), *application_tools]) leaves the application's tools and their strict settings as bound.
  • powercontext_langchain.McpToolDefinitionsMiddleware(tools): for create_agent. On each model call it replaces the given tools, by name, with those definitions in the ModelRequest. The agent still executes the tools it was created with; tools that were not given are unchanged.
  • tests/langchain_middleware/test_mcp_tools.py: request-capture regressions through the real ChatOpenAI interface (httpx.MockTransport) with the full catalog from scripts/mcp_catalog.py, for bind_tools, create_agent (sync, including one executed tool call and the second request) and create_agent (async). Each checks strict: false and parameters == inputSchema for every MCP tool; the bind_tools and sync agent tests also check that application tools are sent as bound (one without strict, and for bind_tools one with strict: true).
  • docs/{en,zh}/docs/integrations/langchain.md, integrations/langchain/README.md: a section on binding the Server's MCP tools with the two helpers.
  • integrations/langchain/examples/bind_mcp_tools.py and examples/README.md: a runnable example that lists the catalog of a running Server over MCP, binds it through mcp_tool_definitions, and executes the MCP tools the model calls. tests/test_langchain_mcp_binding.py covers its binding function.

PowerContextMiddleware, the LangGraph adapter and the MCP schemas are unchanged.

Are there any user-facing changes?

Two new public names in powercontext-langchain (mcp_tool_definitions, McpToolDefinitionsMiddleware), documentation and an example. No change to existing behavior.

How was this change tested?

Python 3.12, langchain-openai==1.6.0, langchain-core==1.8.1, langchain==1.5.0, openai==3.42.1. Provider: gpt-4.1-mini on api.openai.com, which the issue had not covered.

Scenario strict omitted Non-strict
Request captured from bind_tools (63 tools, 129 anyOf), replayed with stream: true, store: false fails 3 of 3 (server_error) passes 3 of 3 (pong). Measured with bind_tools(..., strict=False) before the helper existed; the helper sends the same per-tool strict: false
The example against a real Server (63 tools over /mcp/) and the real provider fails 2 of 2 (HTTP 500) passes with mcp_tool_definitions
create_agent with tools loaded by langchain-mcp-adapters from the real Server, real provider fails 2 of 2 (HTTP 500) passes 2 of 2 with McpToolDefinitionsMiddleware; the model calls an MCP tool, the agent executes it and answers from the result
  • uv run pytest tests/langchain_middleware tests/test_langchain_mcp_binding.py -q: 15 passed, 1 skipped. All four binding tests fail when "strict": False is removed from the helper.
  • make check: passes. make docs-test: passes.

Not covered: Azure OpenAI (not re-run; see the issue), McpToolDefinitionsMiddleware combined with PowerContextMiddleware in one agent, and non-OpenAI chat models.

AI usage statement

Investigated, written and verified with Claude Code (Claude Fable 5), reviewed by the author.


中文摘要:ChatOpenAI(use_responses_api=True) 默认不发送 strict 字段,Responses API 会按 strict 模式处理,带完整 PowerContext MCP 工具目录的请求可能失败。本 PR 按 #2999 的结论在 powercontext-langchain 中提供两个公开入口:mcp_tool_definitions(用于 bind_tools)和 McpToolDefinitionsMiddleware(用于 create_agent),只作用于调用方传入的工具,schema 不变,应用的其他工具及其 strict 设置不受影响。附带截获请求的回归测试、中英文文档和可运行示例。在 api.openai.com 的 gpt-4.1-mini 上验证:不使用时失败,使用后通过。

🤖 Generated with Claude Code

…ses API

ChatOpenAI(use_responses_api=True).bind_tools() leaves strict out of every
function tool by default. The Responses API then applies strict mode, and a
request that carries the full PowerContext MCP catalog can fail.

Document bind_tools(..., strict=False) in both locales, add a runnable example
that binds the catalog of a running Server and executes the tools the model
calls, and cover the outgoing request with a regression test.

Closes oceanbase#2999

Co-Authored-By: Claude <noreply@anthropic.com>
frf12 and others added 2 commits December 3, 2026 17:29
mcp_tool_definitions() returns function tool definitions that set strict to
false themselves and keep each input schema, so they can be bound next to the
application's own tools without changing those. McpToolDefinitionsMiddleware
applies the same definitions to the tools create_agent binds itself, while the
agent still executes the tools it was created with.

Cover both paths with request-capture tests, document them in both locales,
and switch the example to the helper.

Refs oceanbase#2999

Co-Authored-By: Claude <noreply@anthropic.com>
@jasper-yl jasper-yl changed the title docs(langchain): bind the Server's MCP tools non-strict on the Responses API feat(langchain): bind the Server's MCP tools non-strict on the Responses API Dec 3, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(langchain): Responses can fail with the full MCP catalog when strict is omitted

2 participants