-
Notifications
You must be signed in to change notification settings - Fork 0
增加 Sampling(委托生成)功能 #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
walterlv
wants to merge
37
commits into
main
Choose a base branch
from
t/lvyi/sampling
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
37 commits
Select commit
Hold shift + click to select a range
3c528be
增加采样功能
walterlv 57c1d0b
简化代码,添加采样功能的单元测试
walterlv c1f4f2f
添加人工测试的 MCP 采样工具
walterlv 27efe1b
解决采样协议不被客户端认识的问题
walterlv c432af1
整理采样异常
walterlv e56fb0a
调整一些代码
walterlv b069d80
处理审查
walterlv e2c67fe
处理审查
walterlv da9028c
整理传输层读取请求或响应的分支
walterlv 3db477f
避免不应该的双语注释
walterlv 694a602
添加更多的调试日志
walterlv 9acc4e6
兼容 MCP Inspector 的采样拒绝
walterlv 3010535
记录客户端错误
walterlv e2eba44
修复指令中的协议版本
walterlv 8dceafe
AI 增加的屎山代码:修复 SSE 消息流向
walterlv 733ca8c
简化传输层的实现
walterlv c6292f6
处理遗留的双语问题
walterlv 10002e6
重构以简化传输层的代码
walterlv 128aa76
更新文档记录
walterlv 5963987
处理审查意见
walterlv 52670d8
处理审查意见
walterlv 4b18f12
处理审查意见
walterlv 3933fbe
处理审查意见
walterlv 19a5117
处理审查意见:修复并发竞态、重复请求ID、SSE心跳、以及初始化请求分类等问题
Copilot c96610f
调整心跳时间
walterlv 9888474
处理审查意见:协议版本运算符比较、SSE并发写互斥、重复请求ID、双语注释、ToArray优化
Copilot 11e92fb
处理审查意见
walterlv a855395
修复 ServerTransportManager:PipeReader 资源释放和大小写不敏感属性查找
Copilot 00bed63
使用更简单的 JsonElement,不需要 PipeReader
walterlv 71549d1
我觉得没必要为了这个不规范的写法浪费性能
walterlv 52f6e76
处理审查意见
walterlv 0710443
添加辅助传输层日志记录的类(避免日志打太多)
walterlv 9b5cd10
统一使用受管理的传输层原始日志记录,避免日志大幅影响控制台输出
walterlv bbb20b0
尝试消除竞态条件
walterlv 3ba6c05
为 GET 添加日志
walterlv 6032afa
记录更详细的原始日志
walterlv b2f9f65
ToString 可以输出文本内容
walterlv File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
samples/DotNetCampus.SampleMcpServer/McpTools/SamplingTool.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| using DotNetCampus.ModelContextProtocol.CompilerServices; | ||
| using DotNetCampus.ModelContextProtocol.Exceptions; | ||
| using DotNetCampus.ModelContextProtocol.Protocol.Messages; | ||
| using DotNetCampus.ModelContextProtocol.Servers; | ||
|
|
||
| namespace DotNetCampus.SampleMcpServer.McpTools; | ||
|
|
||
| public class SamplingTool | ||
| { | ||
| /// <summary> | ||
| /// 通过客户端的 LLM 进行采样,将 prompt 发送给客户端,获取 LLM 响应并返回。 | ||
| /// 用于人工验证 sampling/createMessage 协议流程是否正常。 | ||
| /// </summary> | ||
| /// <param name="prompt">发送给 LLM 的提示词</param> | ||
| /// <param name="maxTokens">最大生成令牌数</param> | ||
| /// <param name="systemPrompt">可选的系统提示词</param> | ||
| /// <param name="context">MCP 工具上下文</param> | ||
| [McpServerTool] | ||
| public async Task<CallToolResult> AskLlm( | ||
| string prompt, | ||
| int maxTokens = 1024, | ||
| string? systemPrompt = null, | ||
| IMcpServerCallToolContext context = null!) | ||
| { | ||
| if (!context.Sampling.IsSupported) | ||
| { | ||
| return CallToolResult.FromError( | ||
| "当前客户端未声明 Sampling 能力。请确保客户端支持 sampling/createMessage 请求。\n" + | ||
| "The connected client has not declared Sampling capability."); | ||
| } | ||
|
|
||
| try | ||
| { | ||
| var result = await context.Sampling.CreateMessageAsync(prompt, maxTokens, systemPrompt, context.CancellationToken); | ||
|
|
||
| var responseText = result.Content switch | ||
| { | ||
| TextContentBlock text => text.Text, | ||
| _ => $"[Non-text content: {result.Content?.GetType().Name}]", | ||
| }; | ||
|
|
||
| return $""" | ||
| Model: {result.Model} | ||
| StopReason: {result.StopReason ?? "unknown"} | ||
| Role: {result.Role} | ||
| --- | ||
| {responseText} | ||
| """; | ||
| } | ||
| catch (McpSamplingRejectedException ex) | ||
| { | ||
| return CallToolResult.FromError( | ||
| $"采样请求被用户拒绝。Sampling request was rejected by the user.\n" + | ||
| $"Code: {ex.ErrorCode}, Message: {ex.RejectionMessage}"); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.