Skip to content

jhderojasUVa/mcp2api

Repository files navigation

MCP2API Adapter

This project is a Python backend application that serves as a universal interface between an MCP (Machine Control Protocol) Server and an OpenAPI-compliant API.

Overview

The application is built with FastAPI, a modern, high-performance web framework for building APIs with Python.

The core idea is to create a dynamic API based on the capabilities discovered from an MCP Server. On startup, the application queries a discovery endpoint on the MCP Server to get a list of its capabilities. For each capability, it dynamically generates a GET and a POST endpoint.

This allows for a flexible and self-configuring API that adapts to the features of any generic MCP Server.

Current Implementation

  • Dynamic Endpoint Generation: The application fetches a list of capabilities from a mock MCP client and creates corresponding API endpoints.
  • OpenAPI Documentation: FastAPI automatically generates interactive API documentation (using Swagger UI), which you can access at /docs.
  • Mocked MCP Client: Currently, the MCP server interaction is mocked. The mcp_client.py file returns a static list of capabilities for demonstration purposes.

Configuration

The application can be configured using environment variables:

  • MOCK: If set to "true", the application will use mock data for the MCP capabilities, and will not attempt to connect to a real MCP server. This is useful for development and testing.
  • MCP_SERVER_URL: The URL of the MCP server's discovery endpoint. This is required if MOCK is not set to "true".

Example:

To run with mock data:

MOCK=true make run

To run with a real MCP server:

MCP_SERVER_URL=http://my-mcp-server.com make run

(Note: on Windows, you might need to set environment variables differently, e.g. set MOCK=true and then make run)

How to Run the Application

  1. Install Dependencies:

    make install
  2. Run the Server:

    make run
  3. Access the API: Once the server is running, you can access the API at http://127.0.0.1:8000.

    • The main endpoint is at /.
    • The interactive API documentation is at /docs.
    • The dynamically generated endpoints will be available at /{capability_name}.

Development

This project uses a Makefile to simplify common development tasks.

  • make install: Install project dependencies using Poetry.
  • make init: Initialize the project by installing the pre-commit hooks.
  • make run: Run the FastAPI application locally in development mode with auto-reloading.
  • make test: Run the test suite in a Docker container. This command will check if Docker is running before executing.
  • make dev: Run the application in a Docker container. This command will also check if Docker is running.

Project Structure

  • pyproject.toml: Defines the project dependencies and metadata for Poetry.
  • mcp2api/: The main application package.
    • main.py: The main FastAPI application file. It handles startup events and endpoint creation.
    • mcp_client.py: Contains the logic for communicating with the MCP server.

Contributing

Contributions are welcome! Please read the CONTRIBUTING.md file for details on how to contribute to this project.

This project follows the Conventional Commits specification. A pre-commit hook is set up to enforce this format. To install the hook, run:

make init

About

A simple POC to create an MCP to API interface to expose MCP Servers to standard APIs

Resources

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published