mcp_server (ABI MCP Server)
What it is
A lightweight MCP server that discovers ABI “agents” from an ABI API OpenAPI spec and exposes them as MCP tools. It proxies tool calls to the ABI API over HTTP and supports multiple MCP transports.
Public API
mcp: FastMCP- Global MCP server instance named
"abi".
- Global MCP server instance named
ABI_API_BASE: str- Base URL for the ABI API (defaults to
http://localhost:9879).
- Base URL for the ABI API (defaults to
get_api_key() -> str- Reads
ABI_API_KEYfrom environment; exits the process if missing.
- Reads
fetch_openapi_spec() -> dict[str, Any]- Fetches
{ABI_API_BASE}/openapi.jsonand returns the parsed JSON (or{}on failure).
- Fetches
extract_agents_from_openapi(openapi_spec: dict[str, Any]) -> list[dict[str, str]]- Scans OpenAPI
pathsfor"/agents/{name}/completion"endpoints and returns agent metadata:name,description(from POSTsummary),function_name(sanitized).
- Scans OpenAPI
agent_name_to_function_name(agent_name: str) -> str- Converts an agent name into a valid Python function name (lowercase, underscores, no leading digit).
call_abi_agent_http(agent_name: str, prompt: str, thread_id: int = 1) -> str(async)- POSTs to
{ABI_API_BASE}/agents/{agent_name}/completionwith JSON{prompt, thread_id}and Bearer auth. - Returns response text (with surrounding quotes stripped) or an error message string on failure.
- POSTs to
create_agent_function(agent_name: str, description: str)- Creates an async tool function
agent_function(prompt, thread_id=1)that callscall_abi_agent_http. - Sets
__name__usingagent_name_to_function_nameand a docstring based ondescription.
- Creates an async tool function
wait_for_api() -> bool(async)- Polls
{ABI_API_BASE}until it returns HTTP 200 (up to 30 retries, 10s delay).
- Polls
register_agents_dynamically()(async)- Optionally waits for API readiness (when
ABI_API_BASEis not localhost). - Fetches OpenAPI spec, extracts agents, registers each as an MCP tool via
mcp.tool()(agent_function).
- Optionally waits for API readiness (when
setup()(async)- Prints startup info, validates API key, and registers agents dynamically.
run()- Runs
setup()and starts the MCP server with transport controlled byMCP_TRANSPORT:"stdio"(default),"sse", or"http"(streamable HTTP).
- Runs
Configuration/Dependencies
Environment variables
ABI_API_KEY(required)- Used as
Authorization: Bearer ...when calling the ABI API.
- Used as
ABI_API_BASE(optional)- Default:
http://localhost:9879
- Default:
MCP_TRANSPORT(optional)stdio(default),sse, orhttp
Python dependencies
httpx(async HTTP client)python-dotenv(load_dotenv()loads.envif present)mcp.server.fastmcp(FastMCP)
Usage
Run as a script
# mcp_server.py is intended to be executed directly
from naas_abi_core.apps.mcp import mcp_server
if __name__ == "__main__":
mcp_server.run()
Minimal environment
export ABI_API_KEY="your_key_here"
export ABI_API_BASE="http://localhost:9879" # optional
export MCP_TRANSPORT="stdio" # or: sse, http
python -m naas_abi_core.apps.mcp.mcp_server
Caveats
- If
ABI_API_KEYis missing,get_api_key()prints instructions and terminates the process viaexit(1). - Agent discovery depends on
{ABI_API_BASE}/openapi.jsonand on paths matching.../agents/<agent>/completion. call_abi_agent_http()returns human-readable error strings on failures (it does not raise), which will surface as tool outputs.- Response handling strips surrounding quotes from
response.text; this assumes the API may return a JSON-quoted string in the body.