Skip to content

armgit / txtai

Public Imported from GitHub
0

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

MCP server updates, closes #899, closes #1073

davidmezzettiefb00067938c
diff --git a/docs/api/mcp.md b/docs/api/mcp.md
index 83108d0b..0c235811 100644
--- a/docs/api/mcp.md
+++ b/docs/api/mcp.md
@@ -5,9 +5,22 @@ The [Model Context Protocol (MCP)](https://modelcontextprotocol.io/introduction)
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
+mcp: boolean|dict
```
+When `mcp` is a boolean, default arguments are used. When `mcp` is a dictionary it supports the following options.
+
+```yaml
+mcp:
+ clientargs: http client options (dict)
+ mcpargs: FastApiMCP options (dict)
+```
+
+See the following links for details on the options.
+
+- [HTTP Client Arguments](https://www.python-httpx.org/api/#asyncclient)
+- [FastApiMCP Arguments](https://github.com/tadata-org/fastapi_mcp/blob/main/fastapi_mcp/server.py#L22)
+
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.
diff --git a/setup.py b/setup.py
index d49dc85d..f0f7abbf 100644
--- a/setup.py
+++ b/setup.py
@@ -44,7 +44,7 @@ extras["ann"] = [
extras["api"] = [
"aiohttp>=3.8.1",
"fastapi>=0.94.0",
- "fastapi-mcp>=0.2.0",
+ "fastapi-mcp>=0.4.0",
"httpx>=0.28.1",
"pillow>=7.1.2",
"python-multipart>=0.0.7",
diff --git a/src/python/txtai/api/application.py b/src/python/txtai/api/application.py
index 57016966..e22112ac 100644
--- a/src/python/txtai/api/application.py
+++ b/src/python/txtai/api/application.py
@@ -8,7 +8,7 @@ import sys
from fastapi import APIRouter, Depends, FastAPI
from fastapi_mcp import FastApiMCP
-from httpx import AsyncClient
+from httpx import AsyncClient, ASGITransport
from .authorization import Authorization
from .base import API
@@ -115,13 +115,37 @@ def lifespan(application):
extension(application)
# Add Model Context Protocol (MCP) service, if applicable
- if config.get("mcp"):
- mcp = FastApiMCP(application, http_client=AsyncClient(timeout=100))
- mcp.mount()
+ createmcp(application, config)
yield
+def createmcp(application, config):
+ """
+ Create a MCP service if application.
+
+ Args:
+ application: FastAPI Application
+ config: configuration
+ """
+
+ mcp = config.get("mcp")
+ if mcp:
+ # HTTP Client arguments
+ defaults = {"base_url": "http://apiserver", "timeout": 100}
+ clientargs = mcp.get("clientargs", {}) if isinstance(mcp, dict) else {}
+ clientargs = {**defaults, **clientargs}
+
+ # Create HTTP client with custom options
+ client = AsyncClient(transport=ASGITransport(app=application, raise_app_exceptions=False), **clientargs)
+
+ # MCP service arguments
+ mcpargs = mcp.get("mcpargs", {}) if isinstance(mcp, dict) else {}
+
+ mcp = FastApiMCP(application, http_client=client, **mcpargs)
+ mcp.mount()
+
+
def start():
"""
Runs application lifespan handler.