Skip to content
Open
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
6 changes: 3 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ jobs:
# Current leg: catches breakage from new Xcode and SDK releases.
- os: macos-latest
swift-version: Xcode default
- os: ubuntu-latest
- os: ubuntu-24.04
swift-version: 6.1.0

runs-on: ${{ matrix.os }}
Expand All @@ -35,13 +35,13 @@ jobs:
- uses: actions/checkout@v4

- name: Prepare Linux apt dependencies
if: matrix.os == 'ubuntu-latest'
if: matrix.os == 'ubuntu-24.04'
run: |
sudo -n apt-get update
sudo -n apt-get install -y --no-install-recommends libcurl4-openssl-dev

- name: Setup Swift on Linux
if: matrix.os == 'ubuntu-latest'
if: matrix.os == 'ubuntu-24.04'
uses: vapor/swiftly-action@v0.2
with:
toolchain: ${{ matrix.swift-version }}
Expand Down
13 changes: 5 additions & 8 deletions Sources/MCP/Base/Value.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ import struct Foundation.Data
import class Foundation.JSONDecoder
import class Foundation.JSONEncoder

/// A codable value.
/// A JSON value. Encoding `.data` produces a data-URL string; decoding that JSON
/// produces `.string`, since an ordinary JSON string does not carry a binary type.
public enum Value: Hashable, Sendable {
case null
case bool(Bool)
case int(Int)
case double(Double)
case string(String)
/// Encodes as a data-URL string. Decoding that JSON produces `.string`.
/// Use `Data.parseDataURL(_:)` to recover the bytes when needed.
case data(mimeType: String? = nil, Data)
case array([Value])
case object([String: Value])
Expand Down Expand Up @@ -120,13 +123,7 @@ extension Value: Codable {
} else if let value = try? container.decode(Double.self) {
self = .double(value)
} else if let value = try? container.decode(String.self) {
if Data.isDataURL(string: value),
case let (mimeType, data)? = Data.parseDataURL(value)
{
self = .data(mimeType: mimeType, data)
} else {
self = .string(value)
}
self = .string(value)
} else if let value = try? container.decode([Value].self) {
self = .array(value)
} else if let value = try? container.decode([String: Value].self) {
Expand Down
60 changes: 20 additions & 40 deletions Tests/MCPTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -124,10 +122,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -174,10 +170,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -230,10 +224,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -321,10 +313,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -401,10 +391,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -481,10 +469,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -521,10 +507,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -656,10 +640,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down Expand Up @@ -717,10 +699,8 @@ struct ClientTests {

// Set up a task to handle the initialize response
let initTask = Task {
try await Task.sleep(for: .milliseconds(10))
if let lastMessage = await transport.sentMessages.last,
let data = lastMessage.data(using: .utf8),
let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
let data = await transport.waitForFirstSentData()
if let request = try? JSONDecoder().decode(Request<Initialize>.self, from: data)
{
let response = Initialize.response(
id: request.id,
Expand Down
15 changes: 15 additions & 0 deletions Tests/MCPTests/Helpers/MockTransport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ actor MockTransport: Transport {
var isConnected = false

private(set) var sentData: [Data] = []
private var firstSendWaiters: [CheckedContinuation<Data, Never>] = []
var sentMessages: [String] {
return sentData.compactMap { data in
guard let string = String(data: data, encoding: .utf8) else {
Expand Down Expand Up @@ -57,6 +58,20 @@ actor MockTransport: Transport {
throw MCPError.transportError(POSIXError(.EIO))
}
sentData.append(message)
let waiters = firstSendWaiters
firstSendWaiters.removeAll()
for waiter in waiters {
waiter.resume(returning: message)
}
}

func waitForFirstSentData() async -> Data {
if let data = sentData.first {
return data
}
return await withCheckedContinuation { continuation in
firstSendWaiters.append(continuation)
}
}

public func receive() -> AsyncThrowingStream<Data, Swift.Error> {
Expand Down
49 changes: 49 additions & 0 deletions Tests/MCPTests/ValueDecodingTests.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import Foundation
import Testing

@testable import MCP

@Suite("Value JSON String Tests")
struct ValueDecodingTests {
@Test("Data-URL-looking strings stay strings in nested JSON")
func dataURLStringsStayStrings() throws {
let strings = [
"data:text/plain,Hello%20World",
"data:text/plain;base64,SGVsbG8=",
"data:application/octet-stream;base64,AAEC/w==",
]
let expected = Value.object(["values": .array(strings.map(Value.string))])
let json = try JSONEncoder().encode(expected)
#expect(try JSONDecoder().decode(Value.self, from: json) == expected)
}

@Test("Explicit data encoding does not change how generic JSON strings decode")
func explicitDataNeedsAnExplicitType() throws {
let data = Value.data(mimeType: "application/octet-stream", Data([0, 1, 2]))
let json = try JSONEncoder().encode(data)
let string = try JSONDecoder().decode(String.self, from: json)
#expect(string == "data:application/octet-stream;base64,AAEC")
#expect(try JSONDecoder().decode(Value.self, from: json) == .string(string))
let parsed = try #require(Data.parseDataURL(string))
#expect(parsed.mimeType == "application/octet-stream")
#expect(parsed.data == Data([0, 1, 2]))
}

@Test("Tool text content survives Value erasure")
func toolTextContentSurvivesValueErasure() throws {
let text = "data:text/plain,Hello%20World"
let result = CallTool.Result(content: [
.text(text: text, annotations: nil, _meta: nil)
])
let erased = try Value(result)
let recovered = try JSONDecoder().decode(
CallTool.Result.self,
from: JSONEncoder().encode(erased)
)
guard case .text(let recoveredText, _, _) = recovered.content.first else {
Issue.record("Expected text content")
return
}
#expect(recoveredText == text)
}
}
Loading