Turn an Alibaba Cloud Baileian Workflow into a Deployable MCP Service

This tutorial walks you through creating a Node.js MCP service for an Alibaba Cloud Baileian workflow, packaging it as an npm module, publishing it, and finally integrating the custom MCP into Baileian to enable AI‑driven market research within intelligent agents.

Alibaba Cloud Developer
Alibaba Cloud Developer
Alibaba Cloud Developer
Turn an Alibaba Cloud Baileian Workflow into a Deployable MCP Service

This article demonstrates how to encapsulate an Alibaba Cloud Baileian workflow as an MCP service, publish it to the npm registry, and integrate it into Baileian agents.

1. Build the MCP Service

Steps:

Write code to wrap the workflow as an MCP service.

Publish the packaged service to the official npm platform.

Create a custom MCP service on the Baileian platform.

Reference the custom MCP service inside an intelligent agent.

1.1 Create a Node.js project npm init -y After initialization, edit package.json as follows:

{
  "name": "bailian-mcp-workflow-server",
  "version": "0.0.1",
  "description": "Bailian MCP server",
  "license": "MIT",
  "author": "Anthropic, PBC (https://anthropic.com)",
  "homepage": "https://modelcontextprotocol.io",
  "bugs": "https://github.com/modelcontextprotocol/servers/issues",
  "type": "module",
  "bin": { "mcp-server-brave-search": "dist/index.js" },
  "files": ["dist"],
  "scripts": {
    "build": "tsc && shx chmod +x dist/*.js",
    "prepare": "npm run build",
    "watch": "tsc --watch"
  },
  "dependencies": { "@modelcontextprotocol/sdk": "1.0.1" },
  "devDependencies": { "@types/node": "^22", "shx": "^0.3.4", "typescript": "^5.6.2" }
}

Create tsconfig.json in the same directory:

{
  "compilerOptions": {
    "target": "ES2022",
    "module": "Node16",
    "moduleResolution": "Node16",
    "strict": true,
    "esModuleInterop": true,
    "skipLibCheck": true,
    "forceConsistentCasingInFileNames": true,
    "resolveJsonModule": true,
    "outDir": "./dist",
    "rootDir": "."
  },
  "include": ["./**/*.ts"],
  "exclude": ["node_modules"]
}

Create an empty index.ts file.

Install dependencies:

npm install

1.2 Wrap Baileian Application API as MCP

Check Baileian API documentation (e.g., https://bailian.console.aliyun.com/?tab=api#/api/?type=app) and use the following curl example as a reference:

curl -X POST https://dashscope.aliyuncs.com/api/v1/apps/YOUR_APP_ID/completion \
  --header "Authorization: Bearer $DASHSCOPE_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{"input":{"prompt":"你是谁?"},"parameters":{},"debug":{}}'

Implement the MCP service in index.ts:

#!/usr/bin/env node
import { Server } from "@modelcontextprotocol/sdk/server/index.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { CallToolRequestSchema, ListToolsRequestSchema } from "@modelcontextprotocol/sdk/types.js";

const MARKET_RESEARCH_ASSISTANT = {
  name: "market_research_tool",
  description: "Intelligent market‑research report generation assistant.",
  inputSchema: {
    type: "object",
    properties: { query: { type: "string", description: "Search query (max 400 chars, 50 words)" } },
    required: ["query"]
  }
};

const server = new Server({ name: "bailian-mcp-workflow-server", version: "0.1.0" }, { capabilities: { tools: {} } });

const DASHSCOPE_API_KEY = process.env.DASHSCOPE_API_KEY!;
if (!DASHSCOPE_API_KEY) { console.error("Error: DASHSCOPE_API_KEY environment variable is required"); process.exit(1); }
const APP_ID = process.env.APP_ID!;
if (!APP_ID) { console.error("Error: APP_ID environment variable is required"); process.exit(1); }

async function performWebMarketResearch(query) {
  const url = `https://dashscope.aliyuncs.com/api/v1/apps/${APP_ID}/completion`;
  const requestBody = { input: { prompt: query }, parameters: {}, debug: {} };
  const response = await fetch(url, {
    method: "POST",
    headers: { "Content-Type": "application/json", "Authorization": `Bearer ${DASHSCOPE_API_KEY}` },
    body: JSON.stringify(requestBody)
  });
  if (!response.ok) {
    throw new Error(`Bailian API error: ${response.status} ${response.statusText}
${await response.text()}`);
  }
  const data = await response.json();
  return JSON.stringify(data);
}

server.setRequestHandler(ListToolsRequestSchema, async () => ({ tools: [MARKET_RESEARCH_ASSISTANT] }));
server.setRequestHandler(CallToolRequestSchema, async (request) => {
  try {
    const { name, arguments: args } = request.params;
    if (!args) throw new Error("No arguments provided");
    switch (name) {
      case "market_research_tool": {
        const { query } = args;
        const results = await performWebMarketResearch(query);
        return { content: [{ type: "text", text: results }], isError: false };
      }
      default:
        return { content: [{ type: "text", text: `Unknown tool: ${name}` }], isError: true };
    }
  } catch (error) {
    return { content: [{ type: "text", text: `Error: ${error instanceof Error ? error.message : String(error)}` }], isError: true };
  }
});

async function runServer() {
  const transport = new StdioServerTransport();
  await server.connect(transport);
  console.error("Bailian MCP Workflow Server running on stdio");
}
runServer().catch((error) => { console.error("Fatal error running server:", error); process.exit(1); });

The server registers the market‑research tool, checks required environment variables ( DASHSCOPE_API_KEY and APP_ID), and forwards queries to the Baileian DashScope API.

1.3 Test the Service Locally

Configure the MCP server in Baileian’s mcpServers JSON (example for Windows):

{
  "mcpServers": {
    "bailian-mcp-workflow-server": {
      "disabled": false,
      "timeout": 300000,
      "command": "cmd",
      "args": ["/c", "node", "D:\\ProgramData\\bailian-mcp-workflow-server\\index.ts"],
      "env": { "DASHSCOPE_API_KEY": "sk-xxxx", "APP_ID": "your_app_id" },
      "transportType": "stdio"
    }
  }
}

After deployment, you can invoke the tool from an intelligent agent and receive JSON‑formatted market‑research results.

2. Package and Publish to npm

1. Register an npm account at npmjs.com and log in locally: npm login 2. Ensure the package name in package.json is unique.

3. Build the project: npm run build 4. Publish the package: npm publish After a successful publish, the package is available at npmjs.com/package/bailian-mcp-workflow-server .

3. Integrate the Published Package into Baileian

1. Create a custom MCP in the Baileian console and paste the same mcpServers configuration, now referencing the npm‑installed command (e.g., npx bailian-mcp-workflow-server).

2. Deploy the MCP service.

3. Add the MCP to an intelligent agent, select the market_research_tool, and run a test query.

Successful execution returns the market‑research JSON, which Baileian automatically renders as readable Markdown.

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.

BackendMCPNode.jsAPIAlibaba CloudnpmBaileian
Alibaba Cloud Developer
Written by

Alibaba Cloud Developer

Alibaba's official tech channel, featuring all of its technology innovations.

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.