Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Sources/MCP/Base/Error.swift
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ extension MCPError: Codable {
case -32602:
self = .invalidParams(unwrapDetail(message))
case -32603:
self = .internalError(unwrapDetail(nil))
self = .internalError(unwrapDetail(message))
case -32042:
// Extract elicitations array from data
var elicitations: [URLElicitationInfo] = []
Expand Down
23 changes: 23 additions & 0 deletions Tests/MCPTests/ResponseTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,29 @@ struct ResponseTests {
}
}

@Test("Internal error preserves the message from an external server")
func testInternalErrorPreservesMessage() throws {
// A peer sends a -32603 error with a message and no `data.detail`, like
// every other JSON-RPC error. Decoding should keep the message instead
// of dropping it, matching the other standard error cases.
let jsonString = """
{"jsonrpc":"2.0","id":"test-id","error":{"code":-32603,"message":"Database connection failed"}}
"""
let data = jsonString.data(using: .utf8)!

let decoder = JSONDecoder()
let decoded = try decoder.decode(Response<TestMethod>.self, from: data)

if case .failure(let decodedError) = decoded.result {
#expect(decodedError.code == -32603)
#expect(
decodedError.localizedDescription
== "Internal error: Database connection failed")
} else {
#expect(Bool(false), "Expected error result")
}
}

@Test("Empty result response encoding")
func testEmptyResultResponseEncoding() throws {
let response = EmptyMethod.response(id: "test-id")
Expand Down