Skip to content

Commit f3f46d6

Browse files
committed
LangChain: Add example using MCP
1 parent 30e6ec6 commit f3f46d6

File tree

4 files changed

+79
-1
lines changed

4 files changed

+79
-1
lines changed

.github/workflows/ml-llamaindex.yml

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,12 @@ jobs:
4343
'3.8',
4444
'3.13',
4545
]
46-
cratedb-version: [ 'nightly' ]
46+
cratedb-version: [
47+
'nightly',
48+
]
49+
cratedb-mcp-version: [
50+
'pr-50',
51+
]
4752

4853
services:
4954
cratedb:
@@ -53,6 +58,15 @@ jobs:
5358
- 5432:5432
5459
env:
5560
CRATE_HEAP_SIZE: 4g
61+
cratedb-mcp:
62+
image: ghcr.io/crate/cratedb-mcp:${{ matrix.cratedb-mcp-version }}
63+
ports:
64+
- 8000:8000
65+
env:
66+
CRATEDB_MCP_TRANSPORT: streamable-http
67+
CRATEDB_MCP_HOST: 0.0.0.0
68+
CRATEDB_MCP_PORT: 8000
69+
CRATEDB_CLUSTER_URL: http://crate:crate@cratedb:4200/
5670

5771
env:
5872
OPENAI_API_KEY: ${{ secrets.OPENAI_API_KEY }}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
Exercise LangChain/LangGraph with the CrateDB MCP server.
3+
4+
## Synopsis
5+
6+
# Install prerequisites.
7+
pip install -U -r requirements.txt
8+
9+
# Start database.
10+
docker run --rm -it --publish=4200:4200 crate/crate:nightly
11+
12+
# Start MCP server.
13+
export CRATEDB_MCP_TRANSPORT=streamable-http
14+
export CRATEDB_MCP_HOST=0.0.0.0
15+
export CRATEDB_MCP_PORT=8000
16+
export CRATEDB_CLUSTER_URL=http://crate:crate@localhost:4200/
17+
docker run --rm -it --network=host --publish=8000:8000 ghcr.io/crate/cratedb-mcp:pr-50
18+
19+
# Run program.
20+
export OPENAI_API_KEY=<your_openai_api_key>
21+
python agent_with_mcp.py
22+
"""
23+
import asyncio
24+
25+
from langchain_mcp_adapters.client import MultiServerMCPClient
26+
from langgraph.prebuilt import create_react_agent
27+
28+
29+
async def amain():
30+
client = MultiServerMCPClient(
31+
{
32+
"cratedb": {
33+
"transport": "streamable_http",
34+
"url": "http://localhost:8000/mcp/"
35+
},
36+
}
37+
)
38+
tools = await client.get_tools()
39+
agent = create_react_agent("openai:gpt-4.1", tools)
40+
41+
QUERY_STR = "What is the average value for sensor 1?"
42+
response = await agent.ainvoke({"messages": QUERY_STR})
43+
answer = response["messages"][-1].content
44+
45+
print("Query was:", QUERY_STR)
46+
print("Answer was:", answer)
47+
48+
49+
def main():
50+
asyncio.run(amain())
51+
52+
53+
if __name__ == "__main__":
54+
main()

topic/machine-learning/llm-langchain/requirements.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
crash
22
google-cloud-aiplatform<2
33
langchain-cratedb<0.1.2
4+
langchain-mcp-adapters<0.2
45
langchain-google-vertexai<3
56
langchain-openai<0.4
67
langchain-text-splitters<0.4
8+
langgraph<0.6
79
pueblo[cli,nlp]>=0.0.10
810
pypdf<6
911
python-dotenv<2

topic/machine-learning/llm-langchain/test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ def reset_database(cratedb):
2929
time.sleep(0.01)
3030

3131

32+
@pytest.fixture(scope="function", autouse=True)
33+
def init_database(cratedb):
34+
"""
35+
Initialize database.
36+
"""
37+
cratedb.run_sql((HERE / "init.sql").read_text())
38+
39+
3240
def pytest_generate_tests(metafunc):
3341
"""
3442
Generate pytest test case per Jupyter Notebook.

0 commit comments

Comments
 (0)