Skip to content

Commit a94accf

Browse files
docs: update agent creation examples and add MCP tools comparison between Haystack and LangChain
1 parent 21c9181 commit a94accf

File tree

1 file changed

+78
-3
lines changed

1 file changed

+78
-3
lines changed

docs-website/docs/overview/migrating-from-langgraphlangchain-to-haystack.mdx

Lines changed: 78 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -416,7 +416,7 @@ print(result["messages"][-1].text)`}</CodeBlock>
416416
417417
from langchain_anthropic import ChatAnthropic
418418
from langchain_core.tools import tool
419-
from langgraph.prebuilt import create_react_agent
419+
from langchain.agents import create_agent
420420
from langchain_core.messages import HumanMessage, SystemMessage
421421
422422
@tool
@@ -434,10 +434,10 @@ model = ChatAnthropic(
434434
model="claude-sonnet-4-5-20250929",
435435
temperature=0,
436436
)
437-
agent = create_react_agent(
437+
agent = create_agent(
438438
model,
439439
tools=[multiply, add],
440-
prompt=SystemMessage(
440+
system_prompt=SystemMessage(
441441
content="You are a helpful assistant that performs arithmetic."
442442
),
443443
)
@@ -575,6 +575,81 @@ print(result)`}</CodeBlock>
575575
</div>
576576
</div>
577577

578+
### Using MCP Tools
579+
580+
Both frameworks support the [Model Context Protocol (MCP)](https://modelcontextprotocol.io), letting agents connect to external tools and services exposed by MCP servers. Haystack provides `MCPTool` and `MCPToolset` through the `mcp-haystack` integration package, which plug directly into the `Agent` component. LangChain's MCP support relies on the separate `langchain-mcp-adapters` package and requires an async workflow throughout.
581+
582+
<div className="code-comparison">
583+
<div className="code-comparison__column">
584+
<CodeBlock language="python" title="Haystack">{`# pip install haystack-ai mcp-haystack anthropic-haystack
585+
586+
from haystack_integrations.tools.mcp import MCPToolset, StdioServerInfo
587+
from haystack.components.agents import Agent
588+
from haystack_integrations.components.generators.anthropic import AnthropicChatGenerator
589+
from haystack.dataclasses import ChatMessage
590+
591+
# Connect to an MCP server - tools are auto-discovered
592+
toolset = MCPToolset(
593+
server_info=StdioServerInfo(
594+
command="uvx",
595+
args=["mcp-server-fetch"],
596+
)
597+
)
598+
599+
agent = Agent(
600+
chat_generator=AnthropicChatGenerator(model="claude-sonnet-4-5-20250929"),
601+
tools=toolset,
602+
system_prompt="You are a helpful assistant that can fetch web content.",
603+
)
604+
605+
result = agent.run(messages=[
606+
ChatMessage.from_user("Fetch the content from https://haystack.deepset.ai")
607+
])
608+
print(result["messages"][-1].text)`}</CodeBlock>
609+
</div>
610+
<div className="code-comparison__column">
611+
<CodeBlock language="python" title="LangGraph + LangChain">{`# pip install langchain-mcp-adapters langgraph langchain-anthropic
612+
613+
import asyncio
614+
from langchain_mcp_adapters.client import MultiServerMCPClient
615+
from langchain.agents import create_agent
616+
from langchain_anthropic import ChatAnthropic
617+
from langchain_core.messages import HumanMessage, SystemMessage
618+
619+
model = ChatAnthropic(model="claude-sonnet-4-5-20250929")
620+
621+
async def run():
622+
client = MultiServerMCPClient(
623+
{
624+
"fetch": {
625+
"command": "uvx",
626+
"args": ["mcp-server-fetch"],
627+
"transport": "stdio",
628+
}
629+
}
630+
)
631+
tools = await client.get_tools()
632+
agent = create_agent(
633+
model,
634+
tools,
635+
system_prompt=SystemMessage(
636+
content="You are a helpful assistant that can fetch web content."
637+
),
638+
)
639+
result = await agent.ainvoke(
640+
{
641+
"messages": [
642+
HumanMessage(content="Fetch the content from https://haystack.deepset.ai")
643+
]
644+
}
645+
)
646+
print(result["messages"][-1].content)
647+
648+
649+
asyncio.run(run())`}</CodeBlock>
650+
</div>
651+
</div>
652+
578653
## Hear from Haystack Users
579654

580655
See how teams across industries use Haystack to power their production AI systems, from RAG applications to agentic workflows.

0 commit comments

Comments
 (0)