-
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
14cf5ba
LangChain: Add example using MCP
amotl 046d8f7
LangChain: Use GA OCI image (head) for `cratedb-mcp`
amotl 45f0704
LangChain: Add missing provisioning notes using `init.sql`
amotl 5e0a338
LangChain: Use general CrateDB usage instructions from `cratedb-about`
amotl 57c2235
LangChain: Address suggestions by CodeRabbit
amotl File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,2 +1,3 @@ | ||
| *.sql | ||
| .env | ||
| *.sql | ||
| !init.sql |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| """ | ||
| 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 | ||
|
Member
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
Member
Author
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. |
||
|
|
||
| # Provision database. | ||
| crash < init.sql | ||
|
|
||
| # 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 | ||
| import os | ||
|
|
||
| from cratedb_about.instruction import GeneralInstructions | ||
| 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( | ||
| model="openai:gpt-4.1", | ||
| tools=tools, | ||
| prompt=GeneralInstructions().render(), | ||
| ) | ||
|
|
||
| QUERY_STR = os.getenv("DEMO_QUERY", "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() | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.