You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
2.8 KiB
2.8 KiB
Transport Agent Notebook
This notebook provides a way to interact with the data-transport MCP server using LangChain and Marimo.
Overview
The notebook.py file implements a TransportAgent class that bridges the gap between Large Language Models (LLMs) and the capabilities of the data-transport system via the Model Context Protocol (MCP). It also demonstrates how to perform direct data access using the transport library.
How it Works
The notebook.py file demonstrates how to integrate MCP with LangChain through the following steps:
- Dynamic Tool Discovery: Instead of hard-coding tool definitions, the
TransportAgentfetches the OpenAPI specification from the MCP server (athttp://localhost:8090/openapi.json). - Schema Mapping: It automatically parses the OpenAPI
componentsandpathsto generate LangChainStructuredToolinstances. This ensures that the agent always has the latest tools available as defined by the MCP server. - Agent Construction: These tools are then passed into a LangChain agent. The agent uses these tools to fulfill user requests, such as listing databases or performing data analysis.
- Interactive Interface: The Marimo framework is used to provide a clean, interactive UI where users can type questions and see the agent's thought process and results.
Code Breakdown
TransportAgent Class
The heart of the notebook. It manages:
- LLM Configuration: Supports
ollama,openai, andazure_openaibackends. - Tool Initialization (
_init_mcp_tools):- Fetches the
openapi.jsonfrom the local MCP server. - Maps API types (string, integer, etc.) to Python types.
- Dynamically creates
StructuredToolobjects with proper descriptions and argument schemas.
- Fetches the
- Agent Initialization (
_init_agent): Combines the chosen LLM with the discovered tools into a functional agent. - Query Handling (
get): Takes a natural language prompt and returns the final response from the agent's execution chain.
How to leverage MCP with LangChain (Summary)
To integrate MCP with LangChain in this project:
- Expose via OpenAPI: The MCP server must provide an
openapi.jsonendpoint. - Automate Tool Creation: Use a helper function (like
_init_mcp_toolsin this notebook) to iterate over the OpenAPI paths and generateStructuredToolobjects. - Bind to Agent: Pass the list of generated tools to a LangChain agent (e.g.,
create_agent). - Dynamic Updates: Because the tools are loaded at runtime from the OpenAPI spec, any update to the MCP server's capabilities is automatically reflected in the agent.
Usage
To run this notebook, ensure the MCP server is running on localhost:8090 and then execute the marimo notebook:
marimo edit www/html/_plugins/notebook.py