ash001 commited on
Commit
b1e93c6
·
1 Parent(s): 74e01b8

add tools as mcp server

Browse files
README.md CHANGED
@@ -63,11 +63,37 @@ Useful for open-world questions outside your databases:
63
  User → Router (LangGraph) → one route:
64
  ├─ SQL Agent (SQLite, read-only)
65
  ├─ Graph Agent (Neo4j → Cypher → answer grounded in results)
66
- ├─ Tools Agent (DuckDuckGo + Wikipedia + arXiv + Calculator)
67
  └─ General (LLM-only)
68
 
69
  ```
70
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  ## 🚀 Run locally (Python)
72
 
73
  ### 1) Install
@@ -190,6 +216,7 @@ Add these as **Secrets**:
190
  | `NEO4J_USERNAME` | (empty) | Neo4j username |
191
  | `NEO4J_PASSWORD` | (empty) | Neo4j password |
192
  | `WIKI_DOC_CHARS` | `2000` | Wikipedia doc truncation size for tools |
 
193
  | `DEBUG` | `0` | Show more intermediate/debug info |
194
 
195
  ---
 
63
  User → Router (LangGraph) → one route:
64
  ├─ SQL Agent (SQLite, read-only)
65
  ├─ Graph Agent (Neo4j → Cypher → answer grounded in results)
66
+ ├─ Tools Agent (DuckDuckGo + Wikipedia + arXiv + Calculator, each an MCP server)
67
  └─ General (LLM-only)
68
 
69
  ```
70
 
71
+ ## 🔌 Tools over MCP
72
+
73
+ Each of the Tools Agent's tools runs as its **own MCP server** ([`mcp_servers/`](mcp_servers/)),
74
+ and the orchestrator is the **MCP client** ([`orchestrator/mcp_tools.py`](orchestrator/mcp_tools.py)):
75
+
76
+ ```
77
+ Tools Agent (LangGraph: assistant ⇄ ToolNode)
78
+ │ MultiServerMCPClient (langchain-mcp-adapters), stdio
79
+ ├─ mcp_servers.web_search_server → duckduckgo_search
80
+ ├─ mcp_servers.wikipedia_server → wikipedia
81
+ ├─ mcp_servers.arxiv_server → arxiv
82
+ └─ mcp_servers.calculator_server → calculator
83
+ ```
84
+
85
+ - At startup the client launches every server and discovers its tools (`tools/list`).
86
+ They come back as ordinary LangChain tools, so `bind_tools` and `ToolNode` are unchanged.
87
+ - Each tool call is a `tools/call` request to that tool's server process.
88
+ - MCP tools are async-only, so the tools graph is driven with `ainvoke`.
89
+ - Set `USE_MCP_TOOLS=0` to fall back to the in-process tools in `orchestrator/tools.py`.
90
+
91
+ Any MCP client can use these servers on its own, e.g. with the MCP Inspector:
92
+
93
+ ```bash
94
+ npx @modelcontextprotocol/inspector python -m mcp_servers.wikipedia_server
95
+ ```
96
+
97
  ## 🚀 Run locally (Python)
98
 
99
  ### 1) Install
 
216
  | `NEO4J_USERNAME` | (empty) | Neo4j username |
217
  | `NEO4J_PASSWORD` | (empty) | Neo4j password |
218
  | `WIKI_DOC_CHARS` | `2000` | Wikipedia doc truncation size for tools |
219
+ | `USE_MCP_TOOLS` | `1` | Serve the Tools Agent's tools from MCP servers |
220
  | `DEBUG` | `0` | Show more intermediate/debug info |
221
 
222
  ---
app.py CHANGED
@@ -1,5 +1,6 @@
1
  from __future__ import annotations
2
 
 
3
  import os
4
  import streamlit as st
5
  from dotenv import load_dotenv
@@ -461,6 +462,8 @@ elif page == "Graph Agent":
461
  elif page == "Tools Agent":
462
  st.title("🧰 Tools Agent (Chat)")
463
  st.write("Tool-Assisted Research Chat (Web + Wikipedia + arXiv + Calculator).")
 
 
464
 
465
  if "tools_messages" not in st.session_state:
466
  st.session_state.tools_messages = [{"role": "assistant", "content": "Ask a question — I'll search web/Wikipedia/arXiv and use tools when needed."}]
@@ -488,7 +491,8 @@ elif page == "Tools Agent":
488
  tools_graph = _tools_graph_cached(settings.llm_model)
489
  msgs = _dict_messages_to_lc(st.session_state.tools_messages)
490
 
491
- out = tools_graph.invoke({"messages": msgs})
 
492
  out_msgs = out.get("messages", []) or []
493
 
494
  last_ai = next((mm for mm in reversed(out_msgs) if isinstance(mm, AIMessage)), None)
@@ -533,6 +537,7 @@ else:
533
  st.success("GROQ_API_KEY is set.")
534
 
535
  st.write("**Current model:**", settings.llm_model)
 
536
  st.write("**SQLite path:**", settings.sqlite_path)
537
 
538
  if settings.neo4j_uri:
 
1
  from __future__ import annotations
2
 
3
+ import asyncio
4
  import os
5
  import streamlit as st
6
  from dotenv import load_dotenv
 
462
  elif page == "Tools Agent":
463
  st.title("🧰 Tools Agent (Chat)")
464
  st.write("Tool-Assisted Research Chat (Web + Wikipedia + arXiv + Calculator).")
465
+ if settings.use_mcp_tools:
466
+ st.caption("🔌 Each tool is served by its own MCP server (stdio).")
467
 
468
  if "tools_messages" not in st.session_state:
469
  st.session_state.tools_messages = [{"role": "assistant", "content": "Ask a question — I'll search web/Wikipedia/arXiv and use tools when needed."}]
 
491
  tools_graph = _tools_graph_cached(settings.llm_model)
492
  msgs = _dict_messages_to_lc(st.session_state.tools_messages)
493
 
494
+ # ainvoke: MCP tools are async-only.
495
+ out = asyncio.run(tools_graph.ainvoke({"messages": msgs}))
496
  out_msgs = out.get("messages", []) or []
497
 
498
  last_ai = next((mm for mm in reversed(out_msgs) if isinstance(mm, AIMessage)), None)
 
537
  st.success("GROQ_API_KEY is set.")
538
 
539
  st.write("**Current model:**", settings.llm_model)
540
+ st.write("**Tools transport:**", "MCP (stdio servers)" if settings.use_mcp_tools else "in-process")
541
  st.write("**SQLite path:**", settings.sqlite_path)
542
 
543
  if settings.neo4j_uri:
mcp_servers/__init__.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ """Each Tools Agent tool, packaged as its own standalone MCP server.
2
+
3
+ Every module here is runnable on its own over stdio, e.g.
4
+
5
+ python -m mcp_servers.wikipedia_server
6
+
7
+ The orchestrator spawns them through orchestrator/mcp_tools.py, but any MCP
8
+ client (Claude Desktop, Cursor, MCP Inspector) can launch them the same way.
9
+ """
mcp_servers/arxiv_server.py ADDED
@@ -0,0 +1,17 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """arXiv paper search as a standalone MCP server (stdio)."""
2
+ from mcp.server.fastmcp import FastMCP
3
+
4
+ from orchestrator.tools import _make_arxiv_tool
5
+
6
+ _arxiv = _make_arxiv_tool(arxiv_k=3)
7
+
8
+ mcp = FastMCP("arxiv", log_level="WARNING")
9
+
10
+
11
+ @mcp.tool(name=_arxiv.name, description=_arxiv.description)
12
+ def arxiv(query: str) -> str:
13
+ return _arxiv.invoke(query)
14
+
15
+
16
+ if __name__ == "__main__":
17
+ mcp.run(transport="stdio")
mcp_servers/calculator_server.py ADDED
@@ -0,0 +1,15 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Safe arithmetic calculator as a standalone MCP server (stdio)."""
2
+ from mcp.server.fastmcp import FastMCP
3
+
4
+ from orchestrator.tools import calculator as _calculator
5
+
6
+ mcp = FastMCP("calculator", log_level="WARNING")
7
+
8
+
9
+ @mcp.tool(name=_calculator.name, description=_calculator.description)
10
+ def calculator(expression: str) -> str:
11
+ return _calculator.invoke(expression)
12
+
13
+
14
+ if __name__ == "__main__":
15
+ mcp.run(transport="stdio")
mcp_servers/web_search_server.py ADDED
@@ -0,0 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """DuckDuckGo web search as a standalone MCP server (stdio)."""
2
+ from mcp.server.fastmcp import FastMCP
3
+ from langchain_community.tools import DuckDuckGoSearchRun
4
+
5
+ _web = DuckDuckGoSearchRun()
6
+
7
+ mcp = FastMCP("web_search", log_level="WARNING")
8
+
9
+
10
+ @mcp.tool(name=_web.name, description=_web.description)
11
+ def duckduckgo_search(query: str) -> str:
12
+ try:
13
+ return _web.invoke(query)
14
+ except Exception as e: # noqa: BLE001 - surface a readable message to the agent
15
+ return f"[web search error] {type(e).__name__}: {e}"
16
+
17
+
18
+ if __name__ == "__main__":
19
+ mcp.run(transport="stdio")
mcp_servers/wikipedia_server.py ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Wikipedia lookup as a standalone MCP server (stdio)."""
2
+ import os
3
+
4
+ from mcp.server.fastmcp import FastMCP
5
+
6
+ from orchestrator.tools import _make_wikipedia_tool
7
+
8
+ # Reuses the in-process tool, so the User-Agent fix and error handling apply here too.
9
+ _wiki = _make_wikipedia_tool(wiki_k=3, wiki_chars=int(os.getenv("WIKI_DOC_CHARS", "2000")))
10
+
11
+ mcp = FastMCP("wikipedia", log_level="WARNING")
12
+
13
+
14
+ @mcp.tool(name=_wiki.name, description=_wiki.description)
15
+ def wikipedia(query: str) -> str:
16
+ return _wiki.invoke(query)
17
+
18
+
19
+ if __name__ == "__main__":
20
+ mcp.run(transport="stdio")
orchestrator/graphs.py CHANGED
@@ -1,5 +1,6 @@
1
  from __future__ import annotations
2
 
 
3
  from typing import Annotated, Any, Dict, List, Literal, TypedDict
4
 
5
  from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
@@ -9,6 +10,7 @@ from langgraph.prebuilt import ToolNode, tools_condition
9
 
10
  from orchestrator.factories import get_llm
11
  from orchestrator.graph_agent import graph_answer
 
12
  from orchestrator.settings import Settings
13
  from orchestrator.sql_agent import sql_answer
14
  from orchestrator.tools import make_web_wiki_arxiv_tools
@@ -137,9 +139,13 @@ a different tool before answering. Never show a raw tool error to the user."""
137
 
138
 
139
  def build_tools_agent_graph(settings: Settings):
140
- tools = make_web_wiki_arxiv_tools(
141
- wiki_chars=settings.wiki_doc_content_chars_max,
142
- )
 
 
 
 
143
  llm = get_llm(settings, temperature=0).bind_tools(tools)
144
 
145
  def assistant(state: RouterState):
@@ -209,7 +215,7 @@ def build_router_graph(settings: Settings):
209
  return {"route": "graph", "messages": [AIMessage(content=str(out["answer"]))], "debug": dbg}
210
 
211
  def tools_node(state: RouterState):
212
- out_state = tools_graph.invoke({"messages": state["messages"]})
213
  out_msgs = out_state.get("messages", [])
214
  tools_used = _extract_tool_names(out_msgs)
215
 
 
1
  from __future__ import annotations
2
 
3
+ import asyncio
4
  from typing import Annotated, Any, Dict, List, Literal, TypedDict
5
 
6
  from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, SystemMessage
 
10
 
11
  from orchestrator.factories import get_llm
12
  from orchestrator.graph_agent import graph_answer
13
+ from orchestrator.mcp_tools import load_mcp_tools
14
  from orchestrator.settings import Settings
15
  from orchestrator.sql_agent import sql_answer
16
  from orchestrator.tools import make_web_wiki_arxiv_tools
 
139
 
140
 
141
  def build_tools_agent_graph(settings: Settings):
142
+ """Run the returned graph with ainvoke: MCP tools have no sync path."""
143
+ if settings.use_mcp_tools:
144
+ tools = load_mcp_tools(settings)
145
+ else:
146
+ tools = make_web_wiki_arxiv_tools(
147
+ wiki_chars=settings.wiki_doc_content_chars_max,
148
+ )
149
  llm = get_llm(settings, temperature=0).bind_tools(tools)
150
 
151
  def assistant(state: RouterState):
 
215
  return {"route": "graph", "messages": [AIMessage(content=str(out["answer"]))], "debug": dbg}
216
 
217
  def tools_node(state: RouterState):
218
+ out_state = asyncio.run(tools_graph.ainvoke({"messages": state["messages"]}))
219
  out_msgs = out_state.get("messages", [])
220
  tools_used = _extract_tool_names(out_msgs)
221
 
orchestrator/mcp_tools.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Load the Tools Agent's tools from MCP servers instead of building them in-process.
2
+
3
+ Each tool runs as its own MCP server (see mcp_servers/), spawned as a subprocess
4
+ over stdio. The adapter discovers them with tools/list and hands them back as
5
+ ordinary LangChain tools, so bind_tools and ToolNode use them unchanged.
6
+
7
+ The adapter's tools are async-only, so the tools graph has to be driven with
8
+ ainvoke (see graphs.py and app.py).
9
+ """
10
+ from __future__ import annotations
11
+
12
+ import asyncio
13
+ import os
14
+ import sys
15
+ from pathlib import Path
16
+
17
+ from langchain_mcp_adapters.client import MultiServerMCPClient
18
+
19
+ from orchestrator.settings import Settings
20
+
21
+ PROJECT_ROOT = Path(__file__).resolve().parents[1]
22
+
23
+ # server name -> module under mcp_servers/
24
+ MCP_SERVERS = {
25
+ "web_search": "mcp_servers.web_search_server",
26
+ "wikipedia": "mcp_servers.wikipedia_server",
27
+ "arxiv": "mcp_servers.arxiv_server",
28
+ "calculator": "mcp_servers.calculator_server",
29
+ }
30
+
31
+
32
+ def mcp_connections(settings: Settings) -> dict:
33
+ # stdio_client forwards only a short allowlist of env vars by default; the
34
+ # servers also need WIKI_*, SSL_CERT_FILE and any proxy settings.
35
+ env = {**os.environ, "WIKI_DOC_CHARS": str(settings.wiki_doc_content_chars_max)}
36
+ return {
37
+ name: {
38
+ "transport": "stdio",
39
+ # Same interpreter as the app, so the servers see the same packages.
40
+ "command": sys.executable,
41
+ "args": ["-m", module],
42
+ "cwd": str(PROJECT_ROOT),
43
+ "env": env,
44
+ }
45
+ for name, module in MCP_SERVERS.items()
46
+ }
47
+
48
+
49
+ def _as_text(tool):
50
+ """Collapse the tool's MCP content blocks into one plain string.
51
+
52
+ The adapter returns a list of content blocks that each carry an `id`, and
53
+ langchain-groq forwards list content to the API verbatim. A plain string is
54
+ what the in-process tools return, so the LLM sees the same thing either way.
55
+ """
56
+ call = tool.coroutine
57
+
58
+ async def call_as_text(**kwargs):
59
+ content, artifact = await call(**kwargs)
60
+ if isinstance(content, list):
61
+ content = "\n".join(
62
+ b["text"] if isinstance(b, dict) else str(b)
63
+ for b in content
64
+ if not isinstance(b, dict) or b.get("type") == "text"
65
+ )
66
+ return content, artifact
67
+
68
+ tool.coroutine = call_as_text
69
+ return tool
70
+
71
+
72
+ def load_mcp_tools(settings: Settings) -> list:
73
+ """Start every server once to list its tools; returns LangChain tools."""
74
+ client = MultiServerMCPClient(mcp_connections(settings))
75
+ return [_as_text(t) for t in asyncio.run(client.get_tools())]
orchestrator/settings.py CHANGED
@@ -21,6 +21,8 @@ class Settings:
21
  # Tool settings
22
  # wiki_top_k_results: int = int(os.getenv("WIKI_TOP_K", "3"))
23
  wiki_doc_content_chars_max: int = int(os.getenv("WIKI_DOC_CHARS", "2000"))
 
 
24
 
25
  # Debug
26
  debug: bool = os.getenv("DEBUG", "0") in ("1","true","True","yes","YES")
 
21
  # Tool settings
22
  # wiki_top_k_results: int = int(os.getenv("WIKI_TOP_K", "3"))
23
  wiki_doc_content_chars_max: int = int(os.getenv("WIKI_DOC_CHARS", "2000"))
24
+ # Serve the Tools Agent's tools from MCP servers (mcp_servers/) instead of in-process.
25
+ use_mcp_tools: bool = os.getenv("USE_MCP_TOOLS", "1") in ("1","true","True","yes","YES")
26
 
27
  # Debug
28
  debug: bool = os.getenv("DEBUG", "0") in ("1","true","True","yes","YES")
requirements.txt CHANGED
@@ -9,6 +9,10 @@ langchain-groq>=0.3.0
9
  langgraph>=0.2.0
10
  langchain-neo4j
11
 
 
 
 
 
12
  # Tools
13
  ddgs
14
  wikipedia>=1.4.0
 
9
  langgraph>=0.2.0
10
  langchain-neo4j
11
 
12
+ # MCP (each Tools Agent tool runs as its own MCP server)
13
+ mcp>=1.9
14
+ langchain-mcp-adapters>=0.1.0
15
+
16
  # Tools
17
  ddgs
18
  wikipedia>=1.4.0