Skip to content

feat: Add state-based configuration support to MCPToolset#2689

Merged
vblagoje merged 11 commits into
mainfrom
mcp_state_io
Jan 16, 2026
Merged

feat: Add state-based configuration support to MCPToolset#2689
vblagoje merged 11 commits into
mainfrom
mcp_state_io

Conversation

@vblagoje
Copy link
Copy Markdown
Member

@vblagoje vblagoje commented Jan 9, 2026

Why

Agents need tools that seamlessly share data through state. MCPTool already supports inputs_from_state and outputs_to_state for automatic parameter injection and output handling. MCPToolset couldn't use these features - breaking Agent workflows that rely on state propagation across multiple MCP tools.

This PR completes MCP's Agent integration story. MCPToolset now dynamically configures state mappings for each discovered tool, enabling the same seamless state-based coordination that MCPTool provides.

What

Added per-tool state configuration to MCPToolset:

  • Added inputs_from_state, outputs_to_state, outputs_to_string parameters (dict mapping tool names to configs)
  • _serialize_state_config() / _deserialize_state_config() helpers handle both nested (outputs_to_state) and flat (outputs_to_string) formats
  • _validate_state_configs() warns on unknown tool names
  • ValueError from Tool parameter validation bubbles up (Haystack >=2.22.0)
  • Updated dependency to haystack-ai>=2.19.0 (supports state params)

How can it be used

from haystack_integrations.tools.mcp import MCPToolset, StdioServerInfo

# Create toolset with per-tool state configuration
toolset = MCPToolset(
    server_info=StdioServerInfo(command="uvx", args=["mcp-server-git"]),
    tool_names=["git_status", "git_diff", "git_log"],
    
    # Map state keys to tool parameters
    inputs_from_state={
        "git_status": {"repository": "repo_path"},
        "git_diff": {"repository": "repo_path"},
        "git_log": {"repository": "repo_path"},
    },
    
    # Map tool outputs to state keys
    outputs_to_state={
        "git_status": {"status_result": {"source": "status"}},
        "git_diff": {"diff_result": {}},
    },
    
    # Convert outputs to strings for LLM
    outputs_to_string={
        "git_diff": {"source": "diff", "handler": format_diff},
    },
)

How did you test it

  • 3 unit tests: state config creation, serialization roundtrip, unknown tool warning
  • 1 validation test: ValueError on invalid parameter (skipped on Haystack <2.22.0)
  • 6 helper tests: serialize/deserialize for both outputs_to_state and outputs_to_string formats

Notes for the reviewer

Parameter validation behavior:

  • Haystack >=2.22.0: ValueError raised at tool creation time for invalid parameter names
  • Haystack 2.19.0-2.21.x: Invalid parameters fail at runtime during tool invocation

@github-actions github-actions Bot added integration:mcp type:documentation Improvements or additions to documentation labels Jan 9, 2026
@vblagoje vblagoje marked this pull request as ready for review January 9, 2026 13:56
@vblagoje vblagoje requested a review from a team as a code owner January 9, 2026 13:56
@vblagoje vblagoje requested review from mpangrazzi and sjrl and removed request for a team January 9, 2026 13:56
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py Outdated
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py Outdated
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py Outdated
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py Outdated
@sjrl
Copy link
Copy Markdown
Contributor

sjrl commented Jan 12, 2026

@vblagoje looking good! I wanted to ask if you could add an integration test using these configs to see if everything works as expected?

vblagoje and others added 5 commits January 12, 2026 13:36
…et.py

Co-authored-by: Sebastian Husch Lee <10526848+sjrl@users.noreply.github.com>
…et.py

Co-authored-by: Sebastian Husch Lee <10526848+sjrl@users.noreply.github.com>
…et.py

Co-authored-by: Sebastian Husch Lee <10526848+sjrl@users.noreply.github.com>
Copy link
Copy Markdown
Contributor

@mpangrazzi mpangrazzi left a comment

Choose a reason for hiding this comment

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

Looks already good! I've added a few comments. +1 on adding an integration test as @sjrl mentioned.

Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py
Comment thread integrations/mcp/src/haystack_integrations/tools/mcp/mcp_toolset.py
Comment thread integrations/mcp/tests/test_mcp_toolset.py Outdated
@tstadel
Copy link
Copy Markdown
Member

tstadel commented Jan 13, 2026

Nice work. Looks good from my end!

@vblagoje
Copy link
Copy Markdown
Member Author

This should be gtg now, please verify last three commits @sjrl

  • Integration test is similar to our canonical state example but using mcp tools instead of inline tool functions
  • Also addressed great comments from @mpangrazzi 🙏

@vblagoje vblagoje requested review from mpangrazzi and sjrl January 15, 2026 15:08
Copy link
Copy Markdown
Contributor

@mpangrazzi mpangrazzi left a comment

Choose a reason for hiding this comment

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

Looks good!

Copy link
Copy Markdown
Contributor

@sjrl sjrl left a comment

Choose a reason for hiding this comment

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

Looks good!

@vblagoje vblagoje merged commit e7346bd into main Jan 16, 2026
10 checks passed
@vblagoje vblagoje deleted the mcp_state_io branch January 16, 2026 10:24
@vblagoje
Copy link
Copy Markdown
Member Author

@tstadel it is ready for you - https://pypi.org/project/mcp-haystack/1.2.0/

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

Labels

integration:mcp type:documentation Improvements or additions to documentation

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add inputs_from_state and outputs_to_state support to MCPToolset

4 participants