Skip to content
This repository was archived by the owner on May 29, 2025. It is now read-only.
This repository was archived by the owner on May 29, 2025. It is now read-only.

Received error: Swift.DecodingError.dataCorrupted #13

@jamesrochabrun

Description

@jamesrochabrun

Some tools responses are not in the expected format and Decoding fails.

Image

Repro

On this repro you can see that using the list_issues will not repro the decoding issue, but if we use list_commits instead we encounter this decoding error, I think the data is coming with some escaped strings that are making this decoding failure.

Received data:
: \"42123b0b64f3ea0b4ea820ea2db8df4b7fef6ea5\",\n        \"url\": \"https://api.github.com/repos/jamesrochabrun/SwiftAnthropic/git/trees/42123b0b64f3ea0b4ea820ea2db8df4b7fef6ea5\"\n      },\n      \"url\": \"https://api.github.com/repos/jamesrochabrun/SwiftAnthropic/git/commits/7424471d5a85dc088137d22dcc5a5903d62864a1\",\n      \"comment_count\": 0,\n      \"verification\": {\n        \"verified\": false,\n        \"reason\": \"unsigned\",\n        \"signature\": null,\n        \"payload\": null,\n        \"verified_at\": null\n      }\n    },\n    \"url\": \"https://api.github.com/repos/jamesrochabrun/SwiftAnthropic/commits/7424471d5a85dc088137d22dcc5a5903d62864a1\",\n    \"html_url\": \"https://github.com/jamesrochabrun/SwiftAnthropic/commit/7424471d5a85dc088137d22dcc5a5903d62864a1\",\n    \"comments_url\": \"https://api.github.com/repos/jamesrochabrun/SwiftAnthropic/commits/7424471d5a85dc088137d22dcc5a5903d62864a1/comments\",\n    \"author\": null,\n    \"committer\": null,\n    \"parents\": [\n      {\n        \"sha\": \"a624abcf464117a0c113043d0b94f294b4bed4d1\",\n        \"url\": \"https://api.github.com/repos/jamesrochabrun/SwiftAnthropic/commits/a624abcf464117a0c113043d0b94f294b4bed4d1\",\n        \"html_url\": \"https://github.com/jamesrochabrun/SwiftAnthropic/commit/a624abcf464117a0c113043d0b94f294b4bed4d1\"\n      }\n    ]\n  }\n]"}]},"jsonrpc":"2.0","id":3}

I believe the decoding failure comes from this function in the JSONRPC package

	public func sendRequest<Request, Response>(_ params: Request, method: String) async throws -> JSONRPCResponse<Response>
	where Request: Encodable, Response: Decodable {
		let (_, data) = try await sendDataRequest(params, method: method)

		return try decoder.decode(JSONRPCResponse<Response>.self, from: data)
	}

Repro code

let client = try await MCPClient(
  info: .init(name: "test", version: "1.0.0"),
  transport: .stdioProcess(
    "npx",
    args: ["-y", "@modelcontextprotocol/server-github"],
    verbose: true),
  capabilities: .init())

let tools = await client.tools
let tool = try tools.value.get().first(where: { $0.name == "list_issues" })! // change this tool for `list_commits` to see erro
print("tool: \(tool)")

// Those parameters can be passed to an LLM that support tool calling.
let description = tool.description
let name = tool.name
let schemaData = try JSONEncoder().encode(tool.inputSchema)
let schema = try JSONSerialization.jsonObject(with: schemaData)

/// The LLM could call into the tool with unstructured JSON input:
let llmToolInput: [String: Any] = [
  "repo": "SwiftAnthropic",
  "owner": "jamesrochabrun"
]
let llmToolInputData = try JSONSerialization.data(withJSONObject: llmToolInput)
let toolInput = try JSONDecoder().decode(JSON.self, from: llmToolInputData)

let result = try await client.callTool(named: name, arguments: toolInput)
if result.isError != true {
  let content = result.content.first?.text?.text
  print("Git status: \(content ?? "")")
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions