Develop an MCP service

更新时间:
复制 MD 格式

You can use the built-in MCP service templates in Function AI to quickly deploy and try out a service, or deploy a custom MCP service from a blank project. After the MCP service is deployed, the platform generates a Server-Sent Events (SSE) connection URL and an MCP service schema for testing.

Create an MCP service

Function Compute provides a concurrent MCP runtime that supports the SSE protocol. You can convert existing MCP services from STDIO mode to remote SSE-compliant services without changing your code. When you create an MCP service using Function Compute, the resulting function includes built-in affinity for MCP SSE scheduling.

Method 1: Deploy from a template

  1. Log on to the Function AI console. On the Explore page, select the MCP Server template category, choose an MCP template, and then click its card. This topic uses the Amap template as an example.

  2. On the template details page, click Deploy Now to open the service configuration page. Enter the API key that you obtained from the Amap Open Platform, and then click Deploy Project.

    The configuration page requires the AMAP_MAPS_API_KEY environment variable.

Method 2: Create from a blank project

  1. Log on to the Function AI console. On the project page, choose Create Project > Create Blank Project, and then enter a custom name and description.

  2. Click the blank project. On the project deployment page, choose New Service > MCP Service.

  3. On the MCP service configuration page, configure the following options, and then click Preview & Deploy.

    • MCP service configuration

      Parameter

      Description

      Example

      Transport type

      Supports hosting MCP services using the STDIO and SSE protocols. If your MCP service code uses the STDIO protocol, Function AI starts an SSE service to proxy STDIO requests. Clients must use SSE to access the service.

      SSE

      Enable authentication

      When you enable authentication, Function AI generates a read-only Bearer token that you can use to add built-in authentication to your service.

      Turn on the switch

      Runtime

      Use the official Node.js, Python, or Java runtimes to host your MCP service on the cloud with a single click and without code modification.

      To deploy with a container, you can also select custom container as the runtime. Then, follow the on-screen instructions to select an image hosted in ACR to start the build.

      Node.js > Node.js 20

      Code repository

      • Use built-in code package

        You can use the Hello World runtime code package built into Function AI to simplify the deployment process. Later, you can write and debug your code in the WebIDE tab of the service details page.

      • Bind a code repository for continuous deployment

        You can also bind a code repository to host your MCP service. This topic uses a GitHub repository as an example. After you bind a Git repository, any push to the specified branch automatically triggers continuous deployment for the service.

        Parameter descriptions:

        • Build environment: For a multi-language project, you can select multiple build environments.

        • Build command: npm build or pip install -t . -r requirements.txt

        • Build cache: Optional. The cache directories for Node.js, Python, and Java runtimes can be set to ~/.npm, ~/.cache, and ~/.m2, respectively.

      When deploying a Node.js project from a GitHub repository, select main for Branch, enter ./ for Code root directory, select Node.js 20 for Build environment, and enter npm install for Build command.

    • Resource configuration

      Parameter

      Description

      Example

      Instance type

      Select or manually enter an appropriate combination of resource specifications based on your business requirements.

      • vCPU: 1

      • Memory: 2 GB

      Scaling policy

      • Default mode

        The most cost-effective mode. In this mode, Function Compute automatically allocates and releases instances for your function and can scale instances down to zero. This mode is suitable for scenarios with irregular and highly fluctuating traffic where some cold starts are acceptable.

      • High-speed mode

        A balanced mode for cost and performance that effectively prevents cold starts. In this mode, you can lock elastic resources in advance by setting the number of provisioned snapshots for rapid scaling. You are charged for vCPU usage only while processing requests, which balances performance and cost. This mode is suitable for latency-sensitive scenarios.

      Note

      Because MCP SSE requests rely on a session mechanism, a request with the same session ID can lose context if routed to a different instance. Therefore, we recommend that you enable High-speed Mode, set one provisioned snapshot, and set the instance limit to 1. This ensures that SSE requests are preferentially routed to the same instance and reduces the probability of these requests being distributed to other elastic instances.

      High-speed mode

    • Network configuration

      Parameter

      Description

      Example

      Allow VPC access

      Specifies whether the function can access resources in a VPC. For more information, see Configure networking.

      On

      VPC

      Required when Allow VPC access is set to Yes. Create a new VPC or select the ID of the VPC that you want to access from the drop-down list.

      fc.auto.create.vpc.1632317****

      vSwitch

      Required when Allow VPC access is set to Yes. Create a new vSwitch or select a vSwitch ID from the drop-down list.

      fc.auto.create.vswitch.vpc-bp1p8248****

      Security group

      Required when Allow VPC access is set to Yes. Create a new security group or select a security group from the drop-down list.

      fc.auto.create.SecurityGroup.vsw-bp15ftbbbbd****

      Disable public access URL

      Turning on this switch restricts access to the MCP service to within the VPC, blocking it from the public internet.

      On

      Allow specified VPCs only

      To restrict calls to the MCP service to specific VPCs, turn on this switch and bind the desired VPCs.

      On

Test the MCP service

After the MCP service is deployed, the platform generates an SSE connection URL for testing the service connection. You can test the service in the Function AI console or locally using the official Inspector tool.

Method 1: Test in the console

  1. On the details page of your MCP service, go to the Service Test tab. Click Test Connection. After the connection succeeds, click Test Tool to start the test.

    If you enabled authentication (using Method 2), Function AI automatically enters the generated Bearer token during the test.

    In the Connection Information section of the Service Test page, click Test Connection. A success message is displayed. In the Debugging Information section, select the Tools tab. You can see the hello tool, with the description "Return string 'Hello World!'". Click Test Tool. A success message is displayed, and the service returns the result {"text": "Hello World!", "type": "text"}.

    If you use an incorrect Bearer token, the test fails.

  2. After the test is successful, select the Monitoring and Logs tabs to view the test results.

Method 2: Test locally with Inspector

  1. On your MCP service's details page, select the Access URL tab and get the public endpoint.

    The public endpoint format is similar to the following example: https://mcp-xxx.cn-hangzhou.fcapp.run/sse, where mcp-xxx is the application identifier.

  2. Run the following command to start the Inspector tool locally:

    npx @modelcontextprotocol/inspector
  3. In the URL input box of the Inspector tool, enter the public endpoint to test the connection.

    Set Transport type to SSE. After a successful connection, a green Connected status appears in the lower-left corner of the interface.

Important

After testing, click Disconnect to close the SSE connection. This prevents ongoing requests from incurring charges for Function Compute resources.

Integrate an MCP service

After you create an MCP service in Function Compute, you can register it in Model Studio by creating a custom MCP service in the Alibaba Cloud Model Studio console. You can then integrate the service into an Agent or a workflow.

Create a custom MCP service

  1. Log on to the Function AI console and find your MCP service. On its project details page, go to the Access URL tab and get the public endpoint.

  2. Log on to the MCP management page of the Alibaba Cloud Model Studio console. Select the Custom Services tab, click + Create MCP Service in the upper-right corner, select Deploy with Script, and then click Deploy Service.

  3. On the Create MCP Service page, set the Service Name, select http as the Installation method, fill in the MCP service configuration, and then click Submit Deployment.

    The following code provides an example of the MCP service configuration. Replace https://****.cn-hangzhou.fcapp.run/sse with the public endpoint that you obtained in Step 1.

    {
      "mcpServers": {
        "test-sse": {
          "url": "https://****.cn-hangzhou.fcapp.run/sse"
        }
      }
    }

Integrate with an Agent

  1. Log on to the application management page of Alibaba Cloud Model Studio, choose + Create Application > Agent, and then click Create Now.

  2. On the Agent page, select a model, add the MCP service that you created in the previous step, and then start testing.

    This topic uses the Qwen-Max model as an example. You can add multiple MCP services, including services that are pre-configured in Alibaba Cloud Model Studio and your custom MCP services.

    For example, after you add the fc-mcp service in the skills section, the UI shows that the service contains 12 tools.

Test the MCP service

  • Large model application: Agent

  • External tool: Custom location service tool get_geolocation

In the input box on the Text Chat interface, enter detailed location information for West Lake in Hangzhou and click the send button to start the test.

Manage variables

MCP services often need to access third-party services, such as Amap or GitHub. This requires access credentials like an API key or a token. We recommend loading these credentials as environment variables or injecting them as startup command parameters. The Function AI platform provides a variable management feature that supports hosting sensitive variables, enabling secure and reliable management of MCP service access credentials. For more information about variables, see Use variables to manage configurations across services.

Configure service variables

On the Service Variables tab, add the following environment variables and enable the Encryption switch for each: AMAP_MAPS_API_KEY, AccessKeyID, and AccessKeySecret.

Load service variables

Method 1: Read from environment variables

By default, the Function AI platform injects configured service variables into the function's environment variables at startup. Your MCP service code can then read these variables directly from the system environment variables.

async function runServer() {
  console.log(`AMAP_MAPS_API_KEY: ${process.env.AMAP_MAPS_API_KEY}`)
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("Amap Maps MCP Server running on stdio");
}
runServer().catch((error) => {
  console.error("Fatal error running server:", error);
  process.exit(1);
});

Method 2: Inject through startup parameters

Service variables configured in the Function AI platform can be referenced using the format ${self.VariableName}. You can modify the command-line arguments in the startup command to inject the variable values during startup.

On the Configuration tab, in the MCP Service Configuration section, edit the startup command to add the variable references to the command-line arguments. For example: python3 -m mcp_server_aliyun_observability --transport sse --transport-port 6080 --access-key-id ${self.AccessKeyID} --access-key-secret ${self.AccessKeySecret}.