Skip to content

Commit c2b3cc3

Browse files
authored
Merge pull request #18 from alvinwatner/feat-mcp-client
feat: integrate langchain-mcp-adapters to support MCP
2 parents 90bfa23 + 37b6c56 commit c2b3cc3

File tree

12 files changed

+769
-54
lines changed

12 files changed

+769
-54
lines changed

README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,58 @@ agent = initialize_agent(
110110
result = agent.run("What actions are available in Gmail?")
111111
print(result)
112112
```
113+
### Using Model Context Protocol (MCP) Tools
113114

115+
The SDK supports integration with [Model Context Protocol (MCP)](https://modelcontextprotocol.io/) servers, allowing you to connect to external tool providers via the MCP protocol.
116+
117+
```python
118+
import asyncio
119+
from langchain.agents import AgentType
120+
from langchain_openai import ChatOpenAI
121+
from pica_langchain import PicaClient, create_pica_agent
122+
from pica_langchain.models import PicaClientOptions
123+
124+
# Configure MCP servers
125+
mcp_options = {
126+
"math": {
127+
"command": "python",
128+
"args": ["./path/to/math_server.py"],
129+
"transport": "stdio",
130+
},
131+
"weather": {
132+
"url": "http://localhost:8000/sse",
133+
"transport": "sse",
134+
}
135+
}
136+
137+
async def main():
138+
# Create client with async initialization
139+
pica_client = await PicaClient.create(
140+
secret="your-pica-secret",
141+
options=PicaClientOptions(
142+
connectors=["*"], # Initialize all available connections
143+
mcp_options=mcp_options, # Add MCP server options
144+
),
145+
)
146+
147+
# Create an agent with both Pica and MCP tools
148+
llm = ChatOpenAI(temperature=0, model="gpt-4o")
149+
agent = create_pica_agent(
150+
client=pica_client,
151+
llm=llm,
152+
agent_type=AgentType.OPENAI_FUNCTIONS,
153+
)
154+
155+
# Use both Pica platform actions and MCP tools
156+
result = await agent.ainvoke({
157+
"input": "Calculate 25 * 17, then check the weather in New York, finally list all connectors Pica supported"
158+
})
159+
160+
print(result["output"])
161+
162+
if __name__ == "__main__":
163+
asyncio.run(main())
164+
```
114165

115166
## Development
116167

examples/mcp_server/math_server.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from mcp.server.fastmcp import FastMCP
2+
3+
mcp = FastMCP("Math")
4+
5+
@mcp.tool()
6+
def add(a: int, b: int) -> int:
7+
"""Add two numbers"""
8+
return a + b
9+
10+
@mcp.tool()
11+
def subtract(a: int, b: int) -> int:
12+
"""Subtract b from a"""
13+
return a - b
14+
15+
@mcp.tool()
16+
def multiply(a: int, b: int) -> int:
17+
"""Multiply two numbers"""
18+
return a * b
19+
20+
@mcp.tool()
21+
def divide(a: int, b: int) -> float:
22+
"""Divide a by b"""
23+
if b == 0:
24+
return "Error: Cannot divide by zero"
25+
return a / b
26+
27+
if __name__ == "__main__":
28+
mcp.run(transport="stdio")
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
"""
2+
Run this MCP server with:
3+
python examples/mcp_server/weather_server.py
4+
or:
5+
python3 examples/mcp_server/weather_server.py
6+
"""
7+
8+
from typing import List
9+
from mcp.server.fastmcp import FastMCP
10+
11+
mcp = FastMCP("Weather")
12+
13+
@mcp.tool()
14+
async def get_weather(location: str) -> str:
15+
"""Get weather for location."""
16+
return "It's always sunny in New York"
17+
18+
if __name__ == "__main__":
19+
mcp.run(transport="sse")

examples/use_with_mcp.py

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
"""
2+
Example demonstrating how to use pica-langchain with MCP.
3+
"""
4+
5+
import os
6+
import sys
7+
import asyncio
8+
9+
from langchain_openai import ChatOpenAI
10+
from langchain.agents import AgentType
11+
from pica_langchain import PicaClient, create_pica_agent
12+
from pica_langchain.models import PicaClientOptions
13+
14+
# Configure MCP servers
15+
mcp_options = {
16+
"math": {
17+
"command": "python",
18+
"args": ["./examples/mcp_server/math_server.py"],
19+
"transport": "stdio",
20+
},
21+
"weather": {
22+
"url": "http://0.0.0.0:8000/sse",
23+
"transport": "sse",
24+
}
25+
}
26+
27+
def get_env_var(name: str) -> str:
28+
"""Get environment variable or exit if not set."""
29+
value = os.environ.get(name)
30+
31+
if not value:
32+
print(f"ERROR: {name} environment variable must be set")
33+
sys.exit(1)
34+
35+
return value
36+
37+
38+
async def main():
39+
pica_client = await PicaClient.create(
40+
secret=get_env_var("PICA_SECRET"),
41+
options=PicaClientOptions(
42+
mcp_options=mcp_options,
43+
),
44+
)
45+
46+
llm = ChatOpenAI(
47+
temperature=0,
48+
model="gpt-4o",
49+
)
50+
51+
# Create an agent with Pica tools
52+
agent = create_pica_agent(
53+
client=pica_client,
54+
llm=llm,
55+
agent_type=AgentType.OPENAI_FUNCTIONS,
56+
)
57+
58+
import signal
59+
60+
def handle_sigterm(*args):
61+
print("\nReceived shutdown signal. Cleaning up...")
62+
sys.exit(0)
63+
64+
signal.signal(signal.SIGTERM, handle_sigterm)
65+
signal.signal(signal.SIGINT, handle_sigterm)
66+
67+
result = await agent.ainvoke(
68+
{"input": ("First, calculate 25 * 17, then check weather in New York, finally list all connectors Pica supported")}
69+
)
70+
71+
print(f"\nWorkflow Result:\n {result}")
72+
73+
74+
if __name__ == "__main__":
75+
asyncio.run(main())

pica_langchain/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from .client import PicaClient
88
from .tools import GetAvailableActionsTool, GetActionKnowledgeTool, ExecuteTool, PromptToConnectPlatformTool
9-
from .utils import create_pica_tools, create_pica_agent
9+
from .utils import create_pica_tools, create_pica_agent, get_tools_from_client
1010
from .models import (
1111
Connection,
1212
ConnectionDefinition,
@@ -25,6 +25,7 @@
2525
"PromptToConnectPlatformTool",
2626
"create_pica_tools",
2727
"create_pica_agent",
28+
"get_tools_from_client",
2829
"Connection",
2930
"ConnectionDefinition",
3031
"AvailableAction",

0 commit comments

Comments
 (0)