mcp_server
What it is
A lightweight HTTP-based MCP server that exposes ABI agents as MCP tools by:
- Fetching the ABI API OpenAPI spec (
/openapi.json) - Discovering agent completion endpoints (
/agents/{agent}/completion) - Dynamically registering each agent as an MCP tool on a
FastMCP("abi")server
Public API
get_api_key() -> str- Reads
ABI_API_KEYfrom environment; exits the process if missing.
- Reads
fetch_openapi_spec() -> dict- Async; GETs
"{ABI_API_BASE}/openapi.json"and returns the parsed JSON (or{}on failure).
- Async; GETs
extract_agents_from_openapi(openapi_spec: dict) -> list[dict[str, str]]- Parses OpenAPI
pathsto find agent completion endpoints and returns agent metadata:name,description(from POSTsummary),function_name.
- Parses OpenAPI
agent_name_to_function_name(agent_name: str) -> str- Normalizes an agent name into a valid Python function name (lowercase, underscores, non-numeric prefix).
call_abi_agent_http(agent_name: str, prompt: str, thread_id: int = 1) -> str- Async; POSTs to
"{ABI_API_BASE}/agents/{agent_name}/completion"with bearer auth. - Returns response text (with surrounding quotes stripped) or a user-facing error string.
- Async; POSTs to
create_agent_function(agent_name: str, description: str)- Creates an async function
(prompt: str, thread_id: int = 1) -> strthat calls the agent via HTTP. - Sets
__name__based onagent_name_to_function_name()and populates__doc__.
- Creates an async function
wait_for_api() -> bool- Async; polls
GET "{ABI_API_BASE}"until it returns HTTP 200 (or times out after 30 attempts).
- Async; polls
register_agents_dynamically()- Async; fetches OpenAPI spec, extracts agents, creates/registers each tool via
mcp.tool()(agent_function). - If
ABI_API_BASEis not localhost, waits for API readiness first.
- Async; fetches OpenAPI spec, extracts agents, creates/registers each tool via
setup()- Async; prints startup info, validates API key, and registers tools dynamically.
run()- Script entry point: runs
setup()then starts MCP server with transport chosen byMCP_TRANSPORT.
- Script entry point: runs
Configuration/Dependencies
Environment variables:
ABI_API_BASE(default:http://localhost:9879)- Base URL for the ABI API server.
ABI_API_KEY(required)- Bearer token used for ABI API calls.
MCP_TRANSPORT(default:stdio)- One of:
stdio(default)ssehttp(mapped to FastMCP transport"streamable-http")
- One of:
Dependencies:
mcp.server.fastmcp.FastMCPhttpx(async HTTP client)python-dotenv(load_dotenv()is called at import time)
Usage
Minimal runnable example (run as a script):
# libs/naas-abi-core/naas_abi_core/apps/mcp/mcp_server.py
from naas_abi_core.apps.mcp.mcp_server import run
if __name__ == "__main__":
run()Typical environment setup:
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_serverCaveats
- If
ABI_API_KEYis missing,get_api_key()prints instructions and terminates the process viaexit(1). - Agent discovery depends on the ABI API OpenAPI spec and endpoints matching
/agents/{name}/completion. - When
ABI_API_BASEis not localhost, the server waits for API readiness (up to ~5 minutes) before attempting discovery; if not ready, it falls back without registering any tools.