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

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:

  1. Dynamic Tool Discovery: Instead of hard-coding tool definitions, the TransportAgent fetches the OpenAPI specification from the MCP server (at http://localhost:8090/openapi.json).
  2. Schema Mapping: It automatically parses the OpenAPI components and paths to generate LangChain StructuredTool instances. This ensures that the agent always has the latest tools available as defined by the MCP server.
  3. 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.
  4. 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, and azure_openai backends.
  • Tool Initialization (_init_mcp_tools):
    • Fetches the openapi.json from the local MCP server.
    • Maps API types (string, integer, etc.) to Python types.
    • Dynamically creates StructuredTool objects with proper descriptions and argument schemas.
  • 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:

  1. Expose via OpenAPI: The MCP server must provide an openapi.json endpoint.
  2. Automate Tool Creation: Use a helper function (like _init_mcp_tools in this notebook) to iterate over the OpenAPI paths and generate StructuredTool objects.
  3. Bind to Agent: Pass the list of generated tools to a LangChain agent (e.g., create_agent).
  4. 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