Implemented JSON-RPC 2.0 protocol handler for MCP communication, enabling AI agents to communicate with ColaFlow using the Model Context Protocol. **Implementation:** - JSON-RPC 2.0 data models (Request, Response, Error, ErrorCode) - MCP protocol models (Initialize, Capabilities, ClientInfo, ServerInfo) - McpProtocolHandler with method routing and error handling - Method handlers: initialize, resources/list, tools/list, tools/call - ASP.NET Core middleware for /mcp endpoint - Service registration and dependency injection setup **Testing:** - 28 unit tests covering protocol parsing, validation, and error handling - Integration tests for initialize handshake and error responses - All tests passing with >80% coverage **Changes:** - Created ColaFlow.Modules.Mcp.Contracts project - Created ColaFlow.Modules.Mcp.Domain project - Created ColaFlow.Modules.Mcp.Application project - Created ColaFlow.Modules.Mcp.Infrastructure project - Created ColaFlow.Modules.Mcp.Tests project - Registered MCP module in ColaFlow.API Program.cs - Added /mcp endpoint via middleware **Acceptance Criteria Met:** ✅ JSON-RPC 2.0 messages correctly parsed ✅ Request validation (jsonrpc: "2.0", method, params, id) ✅ Error responses conform to JSON-RPC 2.0 spec ✅ Invalid requests return proper error codes (-32700, -32600, -32601, -32602) ✅ MCP initialize method implemented ✅ Server capabilities returned (resources, tools, prompts) ✅ Protocol version negotiation works (1.0) ✅ Request routing to method handlers ✅ Unit test coverage > 80% ✅ All tests passing **Story**: docs/stories/sprint_5/story_5_1.md 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
101 lines
2.9 KiB
C#
101 lines
2.9 KiB
C#
using System.Text.Json.Serialization;
|
|
|
|
namespace ColaFlow.Modules.Mcp.Contracts.JsonRpc;
|
|
|
|
/// <summary>
|
|
/// JSON-RPC 2.0 response object
|
|
/// </summary>
|
|
public class JsonRpcResponse
|
|
{
|
|
/// <summary>
|
|
/// A String specifying the version of the JSON-RPC protocol. MUST be exactly "2.0"
|
|
/// </summary>
|
|
[JsonPropertyName("jsonrpc")]
|
|
public string JsonRpc { get; set; } = "2.0";
|
|
|
|
/// <summary>
|
|
/// This member is REQUIRED on success. Must not exist if there was an error
|
|
/// </summary>
|
|
[JsonPropertyName("result")]
|
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
public object? Result { get; set; }
|
|
|
|
/// <summary>
|
|
/// This member is REQUIRED on error. Must not exist if there was no error
|
|
/// </summary>
|
|
[JsonPropertyName("error")]
|
|
[JsonIgnore(Condition = JsonIgnoreCondition.WhenWritingNull)]
|
|
public JsonRpcError? Error { get; set; }
|
|
|
|
/// <summary>
|
|
/// This member is REQUIRED. It MUST be the same as the value of the id member in the Request Object.
|
|
/// If there was an error in detecting the id in the Request object (e.g. Parse error/Invalid Request), it MUST be Null.
|
|
/// </summary>
|
|
[JsonPropertyName("id")]
|
|
public object? Id { get; set; }
|
|
|
|
/// <summary>
|
|
/// Creates a success response
|
|
/// </summary>
|
|
public static JsonRpcResponse Success(object? result, object? id)
|
|
{
|
|
return new JsonRpcResponse
|
|
{
|
|
Result = result,
|
|
Id = id
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an error response
|
|
/// </summary>
|
|
public static JsonRpcResponse CreateError(JsonRpcError error, object? id)
|
|
{
|
|
return new JsonRpcResponse
|
|
{
|
|
Error = error,
|
|
Id = id
|
|
};
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a ParseError response (id is null because request couldn't be parsed)
|
|
/// </summary>
|
|
public static JsonRpcResponse ParseError(string? details = null)
|
|
{
|
|
return CreateError(JsonRpcError.ParseError(details), null);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an InvalidRequest response
|
|
/// </summary>
|
|
public static JsonRpcResponse InvalidRequest(string? details = null, object? id = null)
|
|
{
|
|
return CreateError(JsonRpcError.InvalidRequest(details), id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a MethodNotFound response
|
|
/// </summary>
|
|
public static JsonRpcResponse MethodNotFound(string method, object? id)
|
|
{
|
|
return CreateError(JsonRpcError.MethodNotFound(method), id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an InvalidParams response
|
|
/// </summary>
|
|
public static JsonRpcResponse InvalidParams(string? details, object? id)
|
|
{
|
|
return CreateError(JsonRpcError.InvalidParams(details), id);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates an InternalError response
|
|
/// </summary>
|
|
public static JsonRpcResponse InternalError(string? details, object? id)
|
|
{
|
|
return CreateError(JsonRpcError.InternalError(details), id);
|
|
}
|
|
}
|