calculate-price MCP Server
MCP server that exposes a price calculation tool (calculatePrice) for computing final prices with discount and tax logic, usable by Codex or any MCP client.
README
MCP vs 普通 Function Calling Demo
这个项目用同一个 calculatePrice 业务函数,对比两种调用方式:
- MCP:把函数暴露成本地
stdioMCP tool,供 Codex 或其他 MCP client 调用。 - 普通 API:把函数暴露成 HTTP API,应用再用 OpenAI Responses API 的 function calling 让模型选择调用参数,最后由应用请求这个 HTTP API。
安装和构建
npm install
npm run build
共享函数
核心函数在 src/price.ts:
- 输入:
unitPrice、quantity、customerTier、可选couponCode、可选taxRate - 输出:小计、会员折扣、优惠码折扣、税费、最终价格
MCP server 和 HTTP API server 都只调用这一个函数。
方式一:通过 MCP 接入 Codex
构建后注册 MCP server:
codex mcp add price-demo -- node /Users/kierantou/Desktop/MCP/dist/mcp-server.js
codex mcp list
codex mcp get price-demo
在 Codex 新会话或当前会话的 /mcp 里确认 price-demo 已启用,然后提问:
帮我计算单价 199、数量 3、gold 用户、优惠码 SAVE10 的最终价格
期望 Codex 调用 calculate_price tool,而不是自己心算。
开发时也可以直接启动 MCP server:
npm run dev:mcp
这个命令会等待 MCP client 通过 stdio 通信,手动运行时没有交互输出是正常的。
方式二:普通 HTTP API + OpenAI Function Calling
先启动价格 API:
npm run dev:api
默认服务地址:
http://localhost:3000
接口:
curl -X POST http://localhost:3000/calculate-price \
-H 'content-type: application/json' \
-d '{"unitPrice":199,"quantity":3,"customerTier":"gold","couponCode":"SAVE10"}'
先设置 API key:
export OPENAI_API_KEY="your_api_key_here"
运行默认示例:
npm run dev:function
也可以传入自己的问题:
npm run dev:function -- "计算单价 88、数量 5、platinum 用户、优惠码 SAVE20 的价格"
脚本会:
- 把
calculate_price的 JSON schema 发给 Responses API。 - 接收模型返回的 function call。
- 请求 HTTP API:
POST /calculate-price。 - 把函数结果发回模型。
- 输出最终自然语言回答。
注意:普通 function calling 里,模型不是自己直接访问任意 HTTP API。模型只会返回一次结构化 function call;真正请求 API 的动作由你的应用代码完成。
对比结论
普通 HTTP API + function calling 更接近传统应用集成方式。业务函数先暴露成 API,应用需要自己声明工具、接收工具调用、请求 API、回传结果。
MCP 更适合把工具做成独立能力。工具 server 可以被 Codex、其他 MCP client 或未来服务复用;client 负责发现工具和调用工具。
第一版只实现本地 stdio MCP。后续如果要给远程 Responses API MCP tool 使用,可以把同一套 calculatePrice 包装成 Streamable HTTP MCP server。
Recommended Servers
playwright-mcp
A Model Context Protocol server that enables LLMs to interact with web pages through structured accessibility snapshots without requiring vision models or screenshots.
Magic Component Platform (MCP)
An AI-powered tool that generates modern UI components from natural language descriptions, integrating with popular IDEs to streamline UI development workflow.
Audiense Insights MCP Server
Enables interaction with Audiense Insights accounts via the Model Context Protocol, facilitating the extraction and analysis of marketing insights and audience data including demographics, behavior, and influencer engagement.
VeyraX MCP
Single MCP tool to connect all your favorite tools: Gmail, Calendar and 40 more.
graphlit-mcp-server
The Model Context Protocol (MCP) Server enables integration between MCP clients and the Graphlit service. Ingest anything from Slack to Gmail to podcast feeds, in addition to web crawling, into a Graphlit project - and then retrieve relevant contents from the MCP client.
Kagi MCP Server
An MCP server that integrates Kagi search capabilities with Claude AI, enabling Claude to perform real-time web searches when answering questions that require up-to-date information.
E2B
Using MCP to run code via e2b.
Neon Database
MCP server for interacting with Neon Management API and databases
Exa Search
A Model Context Protocol (MCP) server lets AI assistants like Claude use the Exa AI Search API for web searches. This setup allows AI models to get real-time web information in a safe and controlled way.
Qdrant Server
This repository is an example of how to create a MCP server for Qdrant, a vector search engine.