-
Notifications
You must be signed in to change notification settings - Fork 9
LangChain: Add example using MCP #1033
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
14cf5ba
046d8f7
45f0704
5e0a338
57c2235
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| *.sql | ||
| .env | ||
| *.sql | ||
| !init.sql |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,54 @@ | ||
| """ | ||
| Exercise LangChain/LangGraph with the CrateDB MCP server. | ||
| ## Synopsis | ||
| # Install prerequisites. | ||
| pip install -U -r requirements.txt | ||
| # Start database. | ||
| docker run --rm -it --publish=4200:4200 crate/crate:nightly | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A step to execute There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for catching this. Fixed with 45f0704. |
||
| # Start MCP server. | ||
| export CRATEDB_MCP_TRANSPORT=streamable-http | ||
| export CRATEDB_MCP_HOST=0.0.0.0 | ||
| export CRATEDB_MCP_PORT=8000 | ||
| export CRATEDB_CLUSTER_URL=http://crate:crate@localhost:4200/ | ||
| docker run --rm -it --network=host --publish=8000:8000 ghcr.io/crate/cratedb-mcp:pr-50 | ||
| # Run program. | ||
| export OPENAI_API_KEY=<your_openai_api_key> | ||
| python agent_with_mcp.py | ||
| """ | ||
| import asyncio | ||
|
|
||
| from langchain_mcp_adapters.client import MultiServerMCPClient | ||
| from langgraph.prebuilt import create_react_agent | ||
|
|
||
|
|
||
| async def amain(): | ||
| client = MultiServerMCPClient( | ||
| { | ||
| "cratedb": { | ||
| "transport": "streamable_http", | ||
| "url": "http://localhost:8000/mcp/" | ||
| }, | ||
| } | ||
| ) | ||
| tools = await client.get_tools() | ||
| agent = create_react_agent("openai:gpt-4.1", tools) | ||
|
||
|
|
||
| QUERY_STR = "What is the average value for sensor 1?" | ||
| response = await agent.ainvoke({"messages": QUERY_STR}) | ||
| answer = response["messages"][-1].content | ||
|
|
||
| print("Query was:", QUERY_STR) | ||
| print("Answer was:", answer) | ||
|
|
||
|
|
||
| def main(): | ||
| asyncio.run(amain()) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| main() | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| DROP TABLE IF EXISTS time_series_data; | ||
|
|
||
| CREATE TABLE IF NOT EXISTS time_series_data ( | ||
| timestamp TIMESTAMP, | ||
| value DOUBLE, | ||
| location STRING, | ||
| sensor_id INT | ||
| ); | ||
|
|
||
| INSERT INTO time_series_data (timestamp, value, location, sensor_id) | ||
| VALUES | ||
| ('2023-09-14T00:00:00', 10.5, 'Sensor A', 1), | ||
| ('2023-09-14T01:00:00', 15.2, 'Sensor A', 1), | ||
| ('2023-09-14T02:00:00', 18.9, 'Sensor A', 1), | ||
| ('2023-09-14T03:00:00', 12.7, 'Sensor B', 2), | ||
| ('2023-09-14T04:00:00', 17.3, 'Sensor B', 2), | ||
| ('2023-09-14T05:00:00', 20.1, 'Sensor B', 2), | ||
| ('2023-09-14T06:00:00', 22.5, 'Sensor A', 1), | ||
| ('2023-09-14T07:00:00', 18.3, 'Sensor A', 1), | ||
| ('2023-09-14T08:00:00', 16.8, 'Sensor A', 1), | ||
| ('2023-09-14T09:00:00', 14.6, 'Sensor B', 2), | ||
| ('2023-09-14T10:00:00', 13.2, 'Sensor B', 2), | ||
| ('2023-09-14T11:00:00', 11.7, 'Sensor B', 2); | ||
|
|
||
| REFRESH TABLE time_series_data; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be adjusted after the next release of
cratedb-mcp.cratedb-mcpandcratedb-mcpocratedb-mcp#50There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved with 046d8f7.