Cloud Native 14 min read

How to Build an Efficient MCP Server on Alibaba Cloud Function Compute

This article walks AI developers through the design, implementation, testing, and cloud deployment of a Model Context Protocol (MCP) Server on Alibaba Cloud Function Compute, covering core capabilities, tool creation, best‑practice guidelines, and practical code examples.

Alibaba Cloud Native
Alibaba Cloud Native
Alibaba Cloud Native
How to Build an Efficient MCP Server on Alibaba Cloud Function Compute

The series of previous articles introduced the MCP protocol and its use in Function Compute (FC)‑hosted MCP Server scenarios; this piece dives deeper into building a high‑performance MCP Server for AI applications.

Background and Design Goals

MCP (Model Context Protocol) standardizes context interaction for AI apps. The MCP Client performs "thinking" while the MCP Server enables "action" by operating external systems and automating workflows.

Core Capabilities of MCP Server

Resources : Expose files or data as URIs, supporting read/write and batch search; accessed via a resource protocol that returns type and content.

Tools : Encapsulate external system operations for LLM calls (with user approval) and return results; accessed via a tool‑call protocol.

Prompts : Provide parameterized business‑scenario prompts to guide the MCP Client.

Note: Not all MCP Clients support the "resource" and "prompt" capabilities; focus on "tool" as the core.

Tool Design Required by the MCP Protocol

tools/list

: Returns the list of available tools and their invocation methods. tools/call: Executes a tool and returns the result. The MCP SDK handles protocol adaptation, so developers only need to define tool parameters and logic.

import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
const server = new McpServer({
  name: "alibabacloud-fc-mcp-server",
  version: "0.0.1",
});
// Add tool implementation
server.tool(
  "toolName",
  "toolDescription",
  {
    arg1: z.string().describe("参数说明,例如:这是一个参数")
  },
  async (args) => {
    const { arg1 } = args;
    return {
      isError: false,
      content: [{ type: "text", text: `成功,得到参数:${arg1}` }]
    };
  }
);
async function main() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
}
main().catch(error => {
  process.stderr.write(JSON.stringify(error));
  process.exit(1);
});

Best Practices for Tool Development

Use clear names and descriptions that accurately reflect functionality and usage scenarios.

Define parameters with detailed JSON‑Schema (type, description, defaults, enums).

Include usage examples in the description to help the LLM invoke the tool correctly.

Implement robust error handling and validation.

Keep each tool atomic—perform a single action without side effects.

Testing the MCP Server

Use the MCP Inspector tool to view the tool list and invocation method. Recommended logging to a file or stderr for easier troubleshooting.

# Build the code project
npm install && npm run build
# Start MCP Server with inspector
npx @modelcontextprotocol/inspector -e ALIBABA_CLOUD_ACCESS_KEY_ID=${ALIBABA_CLOUD_ACCESS_KEY_ID} -e ALIBABA_CLOUD_ACCESS_KEY_SECRET=${ALIBABA_CLOUD_ACCESS_KEY_SECRET} node build/index.js
# Open the debug page shown by the inspector, e.g. http://127.0.0.1:6274

Example log output:

console.error("这是一行需要记录的日志")

Integration with Cursor IDE

Configure the MCP Server in Cursor’s mcpServers section:

{
  "mcpServers": {
    "alibabacloud-fc-mcp-server": {
      "command": "node",
      "args": ["${absolute-path-to-your-project}/build/index.js"],
      "env": {
        "ALIBABA_CLOUD_ACCESS_KEY_ID": "${your-access-key-id}",
        "ALIBABA_CLOUD_ACCESS_KEY_SECRET": "${your-access-key-secret}"
      }
    }
  }
}

Deploying the MCP Server to the Cloud

Switch from stdio to sse protocol for cloud communication.

Code packages cannot be accessed via local paths; they must be uploaded (e.g., via OSS) and referenced with a codeUri parameter.

Remote‑mode tool example:

if (remoteMode) {
  server.tool(
    "put-custom-runtime-function",
    "Provide a downloadable zip matching Alibaba Cloud custom runtime, create or update the function, and deploy the code.",
    { codeUri: codeUriSchema, functionName: functionNameSchema, region: regionSchema },
    async (args) => {
      const { codeUri } = args;
      if (!codeUri) return { isError: true, content: [{ type: "text", text: `执行失败,需要指定codeUri参数` }] };
      const location = path.join(os.tmpdir(), `code-${Date.now()}.zip`);
      await downloadFile(codeUri, location);
      if (!fs.existsSync(location)) return { isError: true, content: [{ type: "text", text: `执行失败,需要指定本地代码工程根路径` }] };
      return await createCustomRuntimeFunction({ ...args, location });
    }
  );
} else {
  // Local mode uses the original "put-custom-runtime-function" tool that works with a local directory.
}

After deployment, configure secure authentication (e.g., bearer token) to avoid exposing AK/SK:

{
  "mcpServers": {
    "aliyun-fc-mcp-server": {
      "url": "https://xxx.cn-hangzhou.fcapp.run/sse",
      "headers": { "Authorization": "Bearer Token" }
    }
  }
}
Note: Some clients such as Cursor IDE currently do not support this authentication method.

Conclusion

The article demonstrates the end‑to‑end process of designing, developing, testing, and cloud‑deploying an FC MCP Server, showing how developers can efficiently build AI‑driven automation pipelines while benefiting from serverless elasticity, reduced operational overhead, and improved security.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

ServerlessAIMCPFunction ComputeCustom Runtime
Alibaba Cloud Native
Written by

Alibaba Cloud Native

We publish cloud-native tech news, curate in-depth content, host regular events and live streams, and share Alibaba product and user case studies. Join us to explore and share the cloud-native insights you need.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.