当前位置: 首页 > news >正文

[mcp: McpSchema]-源码分析

JSONRPCMessage

JSONRPCMessage 是一个密封接口,表示 JSON-RPC 协议中的请求、通知和响应消息,提供统一的协议版本标识方法。

public sealed interface JSONRPCMessage permits JSONRPCRequest, JSONRPCNotification, JSONRPCResponse {String jsonrpc();
}

JSONRPCRequest

JSONRPCRequest 是 JSON-RPC 2.0 协议中的一种消息类型,用于客户端向服务器发送请求,包含方法、参数和请求ID。

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record JSONRPCRequest( // @formatter:off@JsonProperty("jsonrpc") String jsonrpc,@JsonProperty("method") String method,@JsonProperty("id") Object id,@JsonProperty("params") Object params) implements JSONRPCMessage {
} // @formatter:on

Request

Request 是一个密封接口,表示不同类型的请求,如初始化、工具调用、消息创建等,提供一个 meta() 方法来获取元数据,并支持通过 progressToken() 方法提取进度令牌。

public sealed interface Requestpermits InitializeRequest, CallToolRequest, CreateMessageRequest, ElicitRequest, CompleteRequest,GetPromptRequest, ReadResourceRequest, SubscribeRequest, UnsubscribeRequest, PaginatedRequest {Map<String, Object> meta();default String progressToken() {if (meta() != null && meta().containsKey("progressToken")) {return meta().get("progressToken").toString();}return null;}
}

JSONRPCNotification

JSONRPCNotification 是 JSON-RPC 2.0 协议中的一种消息类型,用于服务器向客户端发送不需要响应的通知。

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record JSONRPCNotification( // @formatter:off@JsonProperty("jsonrpc") String jsonrpc,@JsonProperty("method") String method,@JsonProperty("params") Object params) implements JSONRPCMessage {
} // @formatter:on

Notification

Notification 是一个密封接口,表示不同类型的通知,如进度通知、日志消息通知、资源更新通知等,提供一个 meta() 方法来获取通知的元数据。

public sealed interface Notificationpermits ProgressNotification, LoggingMessageNotification, ResourcesUpdatedNotification {Map<String, Object> meta();}

JSONRPCResponse

JSONRPCResponse 是 JSON-RPC 2.0 协议中的一种消息类型,用于表示服务器对客户端请求的响应,包含结果或错误信息。

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonIgnoreProperties(ignoreUnknown = true)
public record JSONRPCResponse( // @formatter:off@JsonProperty("jsonrpc") String jsonrpc,@JsonProperty("id") Object id,@JsonProperty("result") Object result,@JsonProperty("error") JSONRPCError error) implements JSONRPCMessage {@JsonInclude(JsonInclude.Include.NON_ABSENT)@JsonIgnoreProperties(ignoreUnknown = true)public record JSONRPCError(@JsonProperty("code") int code,@JsonProperty("message") String message,@JsonProperty("data") Object data) {}
}// @formatter:on

Result

Result 是一个密封接口,用于表示不同操作的结果,并提供一个 meta() 方法来获取相关的元数据。

public sealed interface Result permits InitializeResult, ListResourcesResult, ListResourceTemplatesResult, ReadResourceResult, ListPromptsResult, GetPromptResult, ListToolsResult, CallToolResult,CreateMessageResult, ElicitResult, CompleteResult, ListRootsResult {Map<String, Object> meta();}

McpClientSession

/*** Sends a JSON-RPC request and returns the response.* @param <T> The expected response type* @param method The method name to call* @param requestParams The request parameters* @param typeRef Type reference for response deserialization* @return A Mono containing the response*/
@Override
public <T> Mono<T> sendRequest(String method, Object requestParams, TypeReference<T> typeRef) {String requestId = this.generateRequestId();return Mono.deferContextual(ctx -> Mono.<McpSchema.JSONRPCResponse>create(sink -> {this.pendingResponses.put(requestId, sink);McpSchema.JSONRPCRequest jsonrpcRequest = new McpSchema.JSONRPCRequest(McpSchema.JSONRPC_VERSION, method,requestId, requestParams);this.transport.sendMessage(jsonrpcRequest).contextWrite(ctx)// TODO: It's most efficient to create a dedicated Subscriber here.subscribe(v -> {}, error -> {this.pendingResponses.remove(requestId);sink.error(error);});})).timeout(this.requestTimeout).handle((jsonRpcResponse, sink) -> {if (jsonRpcResponse.error() != null) {logger.error("Error handling request: {}", jsonRpcResponse.error());sink.error(new McpError(jsonRpcResponse.error()));}else {if (typeRef.getType().equals(Void.class)) {sink.complete();}else {sink.next(this.transport.unmarshalFrom(jsonRpcResponse.result(), typeRef));}}});
}/*** Sends a JSON-RPC notification.* @param method The method name for the notification* @param params The notification parameters* @return A Mono that completes when the notification is sent*/
@Override
public Mono<Void> sendNotification(String method, Object params) {McpSchema.JSONRPCNotification jsonrpcNotification = new McpSchema.JSONRPCNotification(McpSchema.JSONRPC_VERSION,method, params);return this.transport.sendMessage(jsonrpcNotification);
}
http://www.dtcms.com/a/306867.html

相关文章:

  • 第1课:向量与矩阵运算
  • 搭建实时足球比分系统从零到一的实战指南
  • Day 4-1: 机器学习算法全面总结
  • 全新AI工具小程序源码 全开源(源码下载)
  • 深入浅出:在 Spring Boot 中构建实时应用 - 全面掌握 WebSocket
  • 解决 Docker 报错 “exec: no such file or directory”
  • 文件权限值的表示方法
  • PHP/Java/Python实现:如何有效防止恶意文件上传
  • Go 语言make函数
  • 输电线路绝缘子泄漏电流在线监测装置的技术解析与应用价值
  • Python读取获取波形图波谷/波峰
  • Directory Opus 使用优化
  • 30道JS高频经典笔试题集合+详解(一)
  • 视觉系统引导冲床冲压:工业自动化的“智能之眼”
  • Dify 从入门到精通(第 4/100 篇):快速上手 Dify 云端:5 分钟创建第一个应用
  • AI培训项目《人工智能大模型应用工程师》课程学习大纲分享!
  • 【sklearn(01)】数据集加载、划分,csv文件创建,特征工程,无量纲化
  • 【编号65】广西地理基础数据(道路、水系、四级行政边界、地级城市、DEM等)
  • 我的世界模组开发教程——资源(1)
  • JeecgBoot(1):前后台环境搭建
  • C#_创建自己的MyList列表
  • 汽车电子控制系统开发的整体安全理念
  • SOA增益谱与ASE光谱的区别
  • SSRF漏洞基础
  • RESTful API和WebSocket的优缺点,各自适合以及不适合什么样的场景
  • LLM 模型部署难题的技术突破:从轻量化到分布式推理的全栈解决方案
  • uni-app webview的message监听不生效(uni.postmessage is not a function)
  • 嵌入式第十五课!!!!指针函数(续)+函数指针+二级指针+指针数组!!!
  • Java 14 新特性解析与代码示例
  • OWSM v4 语音识别学习笔记