Build a Custom Alibaba Cloud OpenAPI MCP Server with Just 10 Lines of Python
This guide explains how to create a lightweight, extensible Alibaba Cloud OpenAPI MCP Server using only ten lines of Python, covering MCP fundamentals, tool registration via OpenAPI metadata, configuration, code examples, and client setup to enable seamless LLM integration with external services.
Introduction
With the rapid adoption of the Model Context Protocol (MCP), existing MCP Servers show limitations such as inflexibility and high token consumption when registering many tools. This article proposes using the alibaba-cloud-ops-mcp-server library to create a custom Alibaba Cloud OpenAPI MCP Server with only ten lines of Python.
Core Concepts
MCP is an open protocol that enables large language models (LLMs) to access external data sources and tools through a standardized client‑server architecture. It solves common LLM pain points, including lack of real‑time data access, inability to perform external actions, and inability to read private files.
The protocol defines several components:
MCP Host: Programs such as Claude Desktop, IDEs, or AI tools that need to access data via MCP.
MCP Client: The protocol client that maintains a 1:1 connection with the server.
MCP Server: A lightweight service that exposes specific functions through the model context protocol.
Local Data Source: Files, databases, or services on the host machine that the server can safely access.
Remote Service: External systems reachable via APIs.
MCP Server can provide three types of capabilities: Resources (read‑only data such as API responses), Tools (functions callable by the LLM), and Prompts (pre‑written templates to guide tasks).
Current MCP Server implementations suffer from rigid tool definitions, excessive token usage, and the need for deep knowledge of Alibaba Cloud OpenAPI when extending functionality.
Implementation
By leveraging the OpenAPI metadata, tools can be generated automatically. The following JSON illustrates how to map services to API names:
{
"service_a": ["api_name_1", "api_name_2"],
"service_b": ["api_name_1", "api_name_2"]
}The core Python code to launch a custom server is:
from mcp.server.fastmcp import FastMCP
from alibaba_cloud_ops_mcp_server.tools import api_tools
def main():
mcp = FastMCP("Example MCP server")
config = {
'ecs': ['DescribeInstances', 'DescribeRegions'],
'vpc': ['DescribeVpcs', 'DescribeVSwitches']
}
api_tools.create_api_tools(mcp, config)
mcp.run(transport='sse')
if __name__ == "__main__":
main()Run the server with environment variables for your Alibaba Cloud credentials:
# Linux/MacOS
ALIBABA_CLOUD_ACCESS_KEY_ID=ak ALIBABA_CLOUD_ACCESS_KEY_SECRET=sk uv run server.py
# Windows
set ALIBABA_CLOUD_ACCESS_KEY_ID=ak && set ALIBABA_CLOUD_ACCESS_KEY_SECRET=sk && uv run server.pyThe server listens on http://127.0.0.1:8000/sse and can be accessed by any MCP client such as Cherry‑Studio or Cline. After configuring the SSE URL, the client can start interacting with the server.
Summary and Outlook
The tutorial demonstrates three main benefits: eliminating repetitive tool creation, removing the need to understand Alibaba Cloud OpenAPI internals, and dramatically lowering the barrier to building custom MCP Servers. Future work aims to move beyond a one‑API‑per‑tool model toward generic tools that can automatically discover, parameterise, and invoke any compliant API.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Alibaba Cloud Developer
Alibaba's official tech channel, featuring all of its technology innovations.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
