How to Build an MCP-Powered Contract Query and Delay Service with Node.js
This article explains how to leverage the Model Context Protocol (MCP) to enable natural-language contract queries and automated contract extensions, detailing the required client‑server interactions, workflow steps, and providing a complete Node.js implementation with code snippets for both query and delay operations.
Background
With the growing adoption of MCP, we explore whether, from a business perspective, MCP‑style interactions can allow users to operate the system simply by describing actions in natural language, delivering lower cost, more convenient, and faster experiences.
Implemented Functions
1. Query contract information based on contract status or contract number:
i. question: "Please help me query contracts pending signature"
ii. Invoke MCP matching via MCP client
iii. After user confirmation, call the contract query MCP server
2. Implement business contract extension handling:
i. Describe in natural language the contract I wish to extend
ii. Invoke MCP matching via MCP client – if the contract is not in the previous list, the large model first queries to ensure the contract is valid
iii. After query completion, automatically invoke the delay MCP server
iiii. Return result: extension successful
iiiii. Business system verification – success 🏅
Code Implementation
import { McpServer } from "@modelcontextprotocol/sdk/server/mcp.js";
import { StdioServerTransport } from "@modelcontextprotocol/sdk/server/stdio.js";
import { z } from "zod";
const QUERY_CONTRACT = "xxxxxxx";
const DELAY_CONTRACT = "xxxxxxx";
// Create server instance
const server = new McpServer({
name: "contract",
version: "1.0.0",
capabilities: {
resources: {},
tools: {},
},
});
// Helper function for making API requests
async function makeRequest(url, params) {
const headers = {
"X-Requested-With": 'XMLHttpRequest',
"content-type": 'application/json',
};
try {
const response = await fetch(url, {
method: 'POST',
headers: headers,
body: JSON.stringify(params),
credentials: 'include',
});
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
return (await response.json());
} catch (error) {
console.error("Error making Contract request:", error);
return null;
}
}
// Format alert data
function formatAlert(feature) {
const props = feature;
return [
`合同号: ${props.contractNumber || "Unknown"}`,
`合同id: ${props.id || "Unknown"}`,
`版本号: ${props.version || "Unknown"}`,
`创建人: ${props.creator || "Unknown"}`,
`合同名称: ${props.name || "Unknown"}`,
`合同主体: ${props.ouName || "Unknown"}`,
`合同类型: ${props.typeName || "Unknown"}`,
`交易方: ${props.refPartiesName || "Unknown"}`,
`有效日期至: ${props.endTime || "Unknown"}`,
"---"
].join("
");
}
const statusDict = {
xx: xx
};
// Register tools
server.tool("获取合同列表", "获取某种状态或某个合同的合同列表", {
state: z.string().describe("两或三字的合同状态 (e.g. xx, xx)"),
contractNumber: z.string().describe("合同号 (e.g. xxxxxxx,xxxxxxxx,xxxxxxx)"),
}, async ({ state, contractNumber }) => {
const statusCode = statusDict[state];
const alertsData = await makeRequest(QUERY_CONTRACT, { status: statusCode, pageIndex: 1, pageSize: 10, searchSource: 0, contractNumber: contractNumber });
// ...省略业务逻辑
});
server.tool("合同延期", "对指定合同进行延期操作", {
contractId: z.string().describe("合同id (e.g. 41556890,41556953)"),
contractNumber: z.string().describe("合同号 (e.g. PE2025A3709738,BEC2025A3709735)"),
version: z.string().describe("版本号 (e.g. 1,2)"),
endTime: z.string().describe("有效日期至 (e.g. 2023-08-15 12:00:00)"),
}, async ({ contractId, contractNumber, version, endTime }) => {
const alertsData = await makeRequest(DELAY_CONTRACT, { endTime: newEndTime, version, contractNumber, contractId });
// ...省略业务逻辑
});
async function main() {
const transport = new StdioServerTransport();
await server.connect(transport);
console.error("Contract MCP Server running on stdio");
}
main().catch((error) => {
console.error("Fatal error in main():", error);
process.exit(1);
});JD Cloud Developers
JD Cloud Developers (Developer of JD Technology) is a JD Technology Group platform offering technical sharing and communication for AI, cloud computing, IoT and related developers. It publishes JD product technical information, industry content, and tech event news. Embrace technology and partner with developers to envision the future.
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.
