@@ -787,11 +787,9 @@ Example using MCPToolset in a Haystack Pipeline:
787787# 1. pip install uvx mcp-server-time # Install required MCP server and tools
788788# 2. export OPENAI_API_KEY="your-api-key" # Set up your OpenAI API key
789789
790- import os
791790from haystack import Pipeline
792- from haystack.components.converters import OutputAdapter
791+ from haystack.components.agents import Agent
793792from haystack.components.generators.chat import OpenAIChatGenerator
794- from haystack.components.tools import ToolInvoker
795793from haystack.dataclasses import ChatMessage
796794from haystack_integrations.tools.mcp import MCPToolset, StdioServerInfo
797795
@@ -805,30 +803,18 @@ mcp_toolset = MCPToolset(
805803 tool_names = [" get_current_time" ] # Only include the get_current_time tool
806804)
807805
808- # Create a pipeline with the toolset
806+ # Create a pipeline with an Agent that owns the tool-calling loop.
807+ # The Agent passes the toolset to the chat generator, executes any requested
808+ # tool calls, and continues until a final answer is produced.
809809pipeline = Pipeline()
810- pipeline.add_component(" llm" , OpenAIChatGenerator(model = " gpt-4o-mini" , tools = mcp_toolset))
811- pipeline.add_component(" tool_invoker" , ToolInvoker(tools = mcp_toolset))
812- pipeline.add_component(
813- " adapter" ,
814- OutputAdapter(
815- template = " {{ initial_msg + initial_tool_messages + tool_messages }} " ,
816- output_type = list[ChatMessage],
817- unsafe = True ,
818- ),
819- )
820- pipeline.add_component(" response_llm" , OpenAIChatGenerator(model = " gpt-4o-mini" ))
821- pipeline.connect(" llm.replies" , " tool_invoker.messages" )
822- pipeline.connect(" llm.replies" , " adapter.initial_tool_messages" )
823- pipeline.connect(" tool_invoker.tool_messages" , " adapter.tool_messages" )
824- pipeline.connect(" adapter.output" , " response_llm.messages" )
810+ pipeline.add_component(" agent" , Agent(chat_generator = OpenAIChatGenerator(model = " gpt-4o-mini" ), tools = mcp_toolset))
825811
826812# Run the pipeline with a user question
827813user_input = " What is the time in New York? Be brief."
828814user_input_msg = ChatMessage.from_user(text = user_input)
829815
830- result = pipeline.run({" llm " : {" messages" : [user_input_msg]}, " adapter " : { " initial_msg " : [user_input_msg]}})
831- print (result[" response_llm " ][" replies " ][0 ].text)
816+ result = pipeline.run({" agent " : {" messages" : [user_input_msg]}})
817+ print (result[" agent " ][" messages " ][- 1 ].text)
832818```
833819
834820You can also use the toolset via Streamable HTTP to talk to remote servers:
@@ -873,7 +859,6 @@ Example using SSE (deprecated):
873859
874860``` python
875861from haystack_integrations.tools.mcp import MCPToolset, SSEServerInfo
876- from haystack.components.tools import ToolInvoker
877862
878863# Create the toolset with an SSE connection
879864sse_toolset = MCPToolset(
@@ -941,7 +926,7 @@ warm_up() -> None
941926
942927Connect and load tools when eager_connect is turned off.
943928
944- This method is automatically called by ` ToolInvoker .warm_up()` and ` Pipeline.warm_up() ` .
929+ This method is automatically called by ` Agent .warm_up()` and ` Pipeline.warm_up() ` .
945930You can also call it directly before using the toolset to ensure all tool schemas
946931are available without performing a real invocation.
947932
0 commit comments