本项目提供了 @模型上下文协议/SDK 的 MCP 服务器实现,能够帮助你轻松配置和使用相关工具,实现广告搜索等功能。
在开始使用 @模型上下文协议/SDK 的 MCP 服务器之前,你可以通过以下步骤进行安装和配置。
你可以使用 npm 进行安装:
npm install @模型上下文协议/mcp-server
然后将其作为命令使用:
npx @模型上下文协议/mcp-server
或者在你的代码中引入并使用:
import { McpServer } from "@模型上下文协议/mcp-server";
const server = new McpServer();
server.start(1234);
编辑 config.json 文件:
{
"apiEndpoint": "http://localhost:3000",
"allowedOrigins": ["*"]
}
然后启动服务器:
npm start
获取所有可用工具的列表:
server.setRequestHandler(ListToolsRequestSchema, () => ({
tools: [
{
name: SEARCH_ADS,
description: "搜索广告",
inputSchema: SearchAdsInputSchema
}
]
}));
调用具体的工具:
server.setRequestHandler(CallToolRequestSchema, async (request) => {
const { name, arguments: args } = request.params;
switch (name) {
case SEARCH_ADS:
return await searchAds(args);
default:
throw new McpError(ErrorCode.MethodNotFound, `未知工具:${name}`);
}
});
搜索平台上的广告:
interface SearchAdsParams {
keyword?: string;
filters: {
priceRange?: [number, number];
category?: string;
};
}
export async function searchAds(params: SearchAdsParams) {
try {
const result = await fetch(`${config.apiEndpoint}/search`, {
method: "POST",
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(params)
});
if (!result.ok) throw new Error("搜索广告失败");
const data = await result.json();
return {
content: [
{
type: "text",
text: JSON.stringify(data, null, 2),
}
],
};
} catch (error) {
throw new McpError(ErrorCode.InternalError, error instanceof Error ? error.message : String(error));
}
}
在项目根目录创建 .env 文件:
DEALX_API_URL=http://localhost:3000
PORT=1234
然后加载环境变量:
import { config } from "dotenv";
config();
create_ad: 创建新广告edit_ad: 编辑现有广告delete_ad: 删除广告get_threads: 获取广告的讨论线程create_thread: 创建新的讨论线程src/
├── config.ts
├── server.ts
└── tools/
└── searchAds.ts
[插入许可证内容]