Skip to content

armgit / txtai

Public Imported from GitHub
0

💡 All-in-one AI framework for semantic search, LLM orchestration and language model workflows

Add Model Context Protocol (MCP) Support, closes #892

davidmezzetti9647c7e91887
diff --git a/docs/api/index.md b/docs/api/index.md
index a570a023..feb1ba78 100644
--- a/docs/api/index.md
+++ b/docs/api/index.md
@@ -110,12 +110,6 @@ The API can be containerized and run. This will bring up an API instance without
[See this section for more information](../cloud/#api).
-## Model Context Protocol (MCP)
-
-The Model Context Protocol is an open standard that enables developers to build secure, two-way connections between their data sources and AI-powered tools.
-
-A txtai API instance can be hosted as a MCP service using [this project](https://github.com/tadata-org/fastapi_mcp).
-
## Supported language bindings
The following programming languages have bindings with the txtai API:
@@ -126,7 +120,7 @@ The following programming languages have bindings with the txtai API:
- [Rust](https://github.com/neuml/txtai.rs)
- [Go](https://github.com/neuml/txtai.go)
-The API also supports hosting [OpenAI-compatible endpoints](./openai).
+The API also supports hosting [OpenAI-compatible](./openai) and [Model Context Protocol (MCP)](./mcp) endpoints.
See the links below for detailed examples covering the API.
diff --git a/docs/api/mcp.md b/docs/api/mcp.md
new file mode 100644
index 00000000..83108d0b
--- /dev/null
+++ b/docs/api/mcp.md
@@ -0,0 +1,21 @@
+# Model Context Protocol
+
+The [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction) is an open standard that enables developers to build secure, two-way connections between their data sources and AI-powered tools.
+
+The API can be configured to handle MCP requests. All enabled endpoints set in the API configuration are automatically added as MCP tools.
+
+```yaml
+mcp: True
+```
+
+Once this configuration option is added, a new route is added to the application `/mcp`.
+
+The [Model Context Protocol Inspector tool](https://www.npmjs.com/package/@modelcontextprotocol/inspector) is a quick way to explore how the MCP tools are exported through this interface.
+
+Run the following and go to the local URL specified.
+
+```
+npx @modelcontextprotocol/inspector node build/index.js
+```
+
+Enter `http://localhost:8000/mcp` to see the full list of tools available.
diff --git a/mkdocs.yml b/mkdocs.yml
index 4b1a9d94..c6e2adae 100644
--- a/mkdocs.yml
+++ b/mkdocs.yml
@@ -127,6 +127,7 @@ nav:
- Configuration: api/configuration.md
- Customization: api/customization.md
- Methods: api/methods.md
+ - Model Context Protocol: api/mcp.md
- OpenAI: api/openai.md
- Security: api/security.md
- Cloud: cloud.md
diff --git a/setup.py b/setup.py
index 70100581..5b9c7bdf 100644
--- a/setup.py
+++ b/setup.py
@@ -40,6 +40,8 @@ extras["ann"] = [
extras["api"] = [
"aiohttp>=3.8.1",
"fastapi>=0.94.0",
+ "fastapi-mcp>=0.2.0",
+ "httpx>=0.28.1",
"pillow>=7.1.2",
"python-multipart>=0.0.7",
"uvicorn>=0.12.1",
diff --git a/src/python/txtai/api/application.py b/src/python/txtai/api/application.py
index 26e4f22a..57016966 100644
--- a/src/python/txtai/api/application.py
+++ b/src/python/txtai/api/application.py
@@ -7,6 +7,8 @@ import os
import sys
from fastapi import APIRouter, Depends, FastAPI
+from fastapi_mcp import FastApiMCP
+from httpx import AsyncClient
from .authorization import Authorization
from .base import API
@@ -112,6 +114,11 @@ def lifespan(application):
extension = APIFactory.get(extension.strip())()
extension(application)
+ # Add Model Context Protocol (MCP) service, if applicable
+ if config.get("mcp"):
+ mcp = FastApiMCP(application, http_client=AsyncClient(timeout=100))
+ mcp.mount()
+
yield
diff --git a/test/python/testapi/testmcp.py b/test/python/testapi/testmcp.py
new file mode 100644
index 00000000..cb3e3c65
--- /dev/null
+++ b/test/python/testapi/testmcp.py
@@ -0,0 +1,59 @@
+"""
+Agent API module tests
+"""
+
+import os
+import tempfile
+import unittest
+
+from unittest.mock import patch
+
+from fastapi.testclient import TestClient
+
+from txtai.api import application
+
+# Configuration for agents
+MCP = """
+mcp: True
+"""
+
+
+# pylint: disable=R0904
+class TestMCP(unittest.TestCase):
+ """
+ API tests for model context protocol (MCP)
+ """
+
+ @staticmethod
+ @patch.dict(os.environ, {"CONFIG": os.path.join(tempfile.gettempdir(), "testapi.yml"), "API_CLASS": "txtai.api.API"})
+ def start():
+ """
+ Starts a mock FastAPI client.
+ """
+
+ config = os.path.join(tempfile.gettempdir(), "testapi.yml")
+
+ with open(config, "w", encoding="utf-8") as output:
+ output.write(MCP)
+
+ # Create new application and set on client
+ application.app = application.create()
+ client = TestClient(application.app)
+ application.start()
+
+ return client
+
+ @classmethod
+ def setUpClass(cls):
+ """
+ Create API client on creation of class.
+ """
+
+ cls.client = TestMCP.start()
+
+ def testMCP(self):
+ """
+ Test that application a /mcp route
+ """
+
+ self.assertTrue(any(route.path == "/mcp" for route in self.client.app.routes))