Skip to content

Python: Support MCP tool binding by reference in declarative YAML - #5052

Closed
hashwnath wants to merge 2 commits into
microsoft:mainfrom
hashwnath:fix/mcp-tool-binding-by-reference-4927
Closed

Python: Support MCP tool binding by reference in declarative YAML#5052
hashwnath wants to merge 2 commits into
microsoft:mainfrom
hashwnath:fix/mcp-tool-binding-by-reference-4927

Conversation

@hashwnath

Copy link
Copy Markdown
Contributor

Summary

Fixes #4927

AgentFactory._parse_tool() ignores self.bindings for the McpTool case, unlike FunctionTool where bindings correctly resolve the callable. This causes pre-built MCP tool dicts passed via AgentFactory(bindings={"product-mcp": mcp_tool}) to be silently ignored — the factory always constructs the tool from YAML fields, resulting in server_url: '' and a 500 error when no url is specified in the YAML.

This PR adds binding resolution for McpTool in _parse_tool(), mirroring the existing FunctionTool pattern:

  1. Explicit bindings — checks tool_resource.bindings list against self.bindings dict
  2. Name-based fallback — matches tool_resource.name directly against self.bindings keys

If a binding resolves, the pre-built tool dict is returned directly. If no binding matches, the existing dict-building logic runs unchanged.

Changes

  • _loader.py: Added binding-by-reference resolution in the McpTool case of _parse_tool()
  • test_declarative_loader.py: Added 4 tests:

Test plan

  • All 723 existing declarative tests pass
  • New tests verify both binding resolution paths and the fallback
  • Ruff format and lint clean
  • Regression test verifies the exact issue scenario (no server_url: '')

…crosoft#4927)

Add binding resolution for McpTool in AgentFactory._parse_tool(), matching
the existing FunctionTool pattern. MCP tools can now be resolved either by
explicit bindings list or by matching the tool name against factory bindings,
allowing pre-built MCP tool dicts to be passed via AgentFactory(bindings=...)
instead of requiring full connection details in every YAML file.

Fixes microsoft#4927

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Fixes declarative YAML tool parsing so MCP tools can be resolved from AgentFactory(bindings=...) (by explicit bindings: entries or by tool name), avoiding accidental construction of invalid MCP configs when YAML omits url.

Changes:

  • Add binding resolution logic for McpTool in AgentFactory._parse_tool(), mirroring the existing FunctionTool binding approach.
  • Add tests covering explicit binding resolution, name-based resolution, the #4927 regression scenario, and fall-through behavior when no binding matches.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
python/packages/declarative/agent_framework_declarative/_loader.py Resolves MCP tool configs from factory bindings (explicit list first, then by tool name), otherwise falls back to existing dict-building behavior.
python/packages/declarative/tests/test_declarative_loader.py Adds targeted tests verifying MCP binding-by-reference behavior and regression coverage for #4927.

@moonbox3

Copy link
Copy Markdown
Contributor

hashwnath please have a look at the failing CI/CD items. Thanks.

Cast bound_tool return value to the declared return type to satisfy
mypy strict mode (warn_return_any). Add edge case tests for explicit
binding priority and None bindings fallthrough.
@hashwnath

Copy link
Copy Markdown
Contributor Author

Fixed the mypy issue! The other CI failures are from the orchestration tests broken on main, unrelated to the declarative changes here.

@moonbox3

Copy link
Copy Markdown
Contributor

Peter Ibekwe (@peibekwe) can you have a look, please?

@eavanvalkenburg

Copy link
Copy Markdown
Member

Closing this as we will spend time on the declarative package soon, the issue will remain open for that.

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

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Support MCP tool binding by reference in declarative YAML

5 participants