正文
:
'text'
,
text
:
`执行错误:
${message}
`
}],
isError
:
true
};
}
}
if
(request.
params
.
name
===
'start_game'
) {
}
if
(!
this
.
currentCase
) {
thrownew
McpError
(
ErrorCode
.
InvalidRequest
,
'请先使用start_game开始游戏'
);
}
if
(request.
params
.
name
===
'get_clue'
) {
}
if
(request.
params
.
name
===
'guess_solution'
) {
}
thrownew
McpError
(
ErrorCode
.
MethodNotFound
,
`未知工具:
${request.params.name}
`
);
});
this
.
server
.
onerror
=
(
error
) =>
console
.
error
(
'[MCP Error]'
, error);
process.
on
(
'SIGINT'
,
async
() => {
await
this
.
server
.
close
();
process.
exit
(
0
);
});
}
let’s go!
富豪 pony 的一个价值连城的宝石失踪了,你能找到小偷吗?
用之前的文章里写的 MCP Client 来加载这个 MCP Server,模型使用的是 Qwen/QwQ-32B , 现在一起来开启探案之旅QwQ!
npm run test /Users/sheng/Desktop/study/mybox/pybox/detectivegames/detective-game-mcp/dist/index.js
效果
直接上效果
整个过程只有第一次交互中,我输入的任务要求开一局侦探游戏吧,帮我解开谜题 ,
剩余的都是 AI 与 MCP Server 自动的多轮交互完成!
我:华生,对这个案件你怎么看?
华生:(一通分析和操作后)小偷是xx!
我:..... 这对吗?这不对吧(我才是主角儿啊)原本以为自己是福尔摩斯,AI 是花生,没想到小丑竟是我自己🤡
交互拆解
第一轮
AI 输出
识别到我的需求,AI 决定调用 start_game 方法,来开始游戏。这是正确的,代码里做了限制,如果不进行开始游戏,则后续的工具无法调用。
MCP Server 返回结果
{"content":[{"type":"text","text":"新案件开始: 宝石失踪案: 富豪 pony 的一个价值连城的宝石失踪了,你能找到小偷吗?"}]}
第二轮
AI 输出
AI: tool_calls {"role":"assistant","content":"新的案件已经开始了:富豪 pony 的一个价值连城的宝石失踪了,你需要找到小偷。让我们一起解开这个谜题。\n\n现在,让我们看看第一个线索是什么。\n","tool_calls":[{"id":"0195ecb445eebcbce3f3f3c7ccd99b4e","type":"function","function":{"name":"get_clue","arguments":"{} }"}}]}
rawArgs is ===== {} }
调用工具 get_clue
参数: {}
AI 获取到了案件信息,是要抓到小偷,找到宝石。这一轮,他决定调用 get_clue 方法来获取线索。
MCP Server 返回结果
{"content":[{"type":"text","text":"线索: 线索在 /mypath/detectivegames/clue.txt 文件里,请想办法获取线索吧"}]}
get_clue 方法,提示了下一个线索的位置,让 AI 自己想办法获取线索。
第三轮
AI 输出