|
| 1 | +import Foundation |
| 2 | +import Testing |
| 3 | + |
| 4 | +@testable import MCP |
| 5 | + |
| 6 | +@Suite("Stateless Mode Tests") |
| 7 | +struct StatelessModeTests { |
| 8 | + |
| 9 | + // MARK: - Transport Property Tests |
| 10 | + |
| 11 | + @Test("MockTransport defaults to supporting server-to-client requests") |
| 12 | + func mockTransportDefaultsToSupportingRequests() async throws { |
| 13 | + let transport = MockTransport() |
| 14 | + let supports = await transport.supportsServerToClientRequests |
| 15 | + #expect(supports == true) |
| 16 | + } |
| 17 | + |
| 18 | + @Test("MockTransport can be configured to not support server-to-client requests") |
| 19 | + func mockTransportCanBeConfiguredToNotSupportRequests() async throws { |
| 20 | + let transport = MockTransport() |
| 21 | + await transport.setSupportsServerToClientRequests(false) |
| 22 | + let supports = await transport.supportsServerToClientRequests |
| 23 | + #expect(supports == false) |
| 24 | + } |
| 25 | + |
| 26 | + // MARK: - Server-to-Client Request Rejection |
| 27 | + |
| 28 | + @Test("listRoots fails immediately when transport does not support server-to-client requests", |
| 29 | + .timeLimit(.minutes(1))) |
| 30 | + func listRootsFailsInStatelessMode() async throws { |
| 31 | + let transport = MockTransport() |
| 32 | + await transport.setSupportsServerToClientRequests(false) |
| 33 | + |
| 34 | + let server = Server(name: "TestServer", version: "1.0") |
| 35 | + |
| 36 | + // Start server and initialize |
| 37 | + try await server.start(transport: transport) |
| 38 | + |
| 39 | + // Queue initialize request with roots capability |
| 40 | + try await transport.queue( |
| 41 | + request: Initialize.request( |
| 42 | + .init( |
| 43 | + protocolVersion: Version.latest, |
| 44 | + capabilities: .init(roots: .init(listChanged: true)), |
| 45 | + clientInfo: .init(name: "TestClient", version: "1.0") |
| 46 | + ) |
| 47 | + )) |
| 48 | + |
| 49 | + // Wait for server to process and respond |
| 50 | + _ = await transport.waitForSentMessageCount(1, timeout: .seconds(5)) |
| 51 | + |
| 52 | + // Queue initialized notification |
| 53 | + try await transport.queue( |
| 54 | + notification: InitializedNotification.message(.init()) |
| 55 | + ) |
| 56 | + |
| 57 | + // Brief pause to ensure notification is processed |
| 58 | + try await Task.sleep(for: .milliseconds(100)) |
| 59 | + |
| 60 | + // listRoots should fail immediately with stateless error |
| 61 | + do { |
| 62 | + _ = try await server.listRoots() |
| 63 | + Issue.record("Expected listRoots to throw in stateless mode") |
| 64 | + } catch let error as MCPError { |
| 65 | + // Verify it's the stateless mode error, not a capability error |
| 66 | + let errorDescription = String(describing: error) |
| 67 | + #expect( |
| 68 | + errorDescription.contains("stateless") || errorDescription.contains("server-to-client"), |
| 69 | + "Expected stateless mode error, got: \(errorDescription)" |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + await server.stop() |
| 74 | + } |
| 75 | + |
| 76 | + @Test("createMessage fails immediately when transport does not support server-to-client requests", |
| 77 | + .timeLimit(.minutes(1))) |
| 78 | + func createMessageFailsInStatelessMode() async throws { |
| 79 | + let transport = MockTransport() |
| 80 | + await transport.setSupportsServerToClientRequests(false) |
| 81 | + |
| 82 | + let server = Server(name: "TestServer", version: "1.0") |
| 83 | + try await server.start(transport: transport) |
| 84 | + |
| 85 | + // Queue initialize request with sampling capability |
| 86 | + try await transport.queue( |
| 87 | + request: Initialize.request( |
| 88 | + .init( |
| 89 | + protocolVersion: Version.latest, |
| 90 | + capabilities: .init(sampling: .init()), |
| 91 | + clientInfo: .init(name: "TestClient", version: "1.0") |
| 92 | + ) |
| 93 | + )) |
| 94 | + |
| 95 | + _ = await transport.waitForSentMessageCount(1, timeout: .seconds(5)) |
| 96 | + |
| 97 | + try await transport.queue( |
| 98 | + notification: InitializedNotification.message(.init()) |
| 99 | + ) |
| 100 | + |
| 101 | + try await Task.sleep(for: .milliseconds(100)) |
| 102 | + |
| 103 | + do { |
| 104 | + _ = try await server.createMessage( |
| 105 | + CreateSamplingMessage.Parameters( |
| 106 | + messages: [Sampling.Message(role: .user, content: .text("Hello"))], |
| 107 | + maxTokens: 100 |
| 108 | + ) |
| 109 | + ) |
| 110 | + Issue.record("Expected createMessage to throw in stateless mode") |
| 111 | + } catch let error as MCPError { |
| 112 | + let errorDescription = String(describing: error) |
| 113 | + #expect( |
| 114 | + errorDescription.contains("stateless") || errorDescription.contains("server-to-client"), |
| 115 | + "Expected stateless mode error, got: \(errorDescription)" |
| 116 | + ) |
| 117 | + } |
| 118 | + |
| 119 | + await server.stop() |
| 120 | + } |
| 121 | + |
| 122 | + @Test("elicit fails immediately when transport does not support server-to-client requests", |
| 123 | + .timeLimit(.minutes(1))) |
| 124 | + func elicitFailsInStatelessMode() async throws { |
| 125 | + let transport = MockTransport() |
| 126 | + await transport.setSupportsServerToClientRequests(false) |
| 127 | + |
| 128 | + let server = Server(name: "TestServer", version: "1.0") |
| 129 | + try await server.start(transport: transport) |
| 130 | + |
| 131 | + // Queue initialize request with elicitation capability |
| 132 | + try await transport.queue( |
| 133 | + request: Initialize.request( |
| 134 | + .init( |
| 135 | + protocolVersion: Version.latest, |
| 136 | + capabilities: .init(elicitation: .init(form: .init())), |
| 137 | + clientInfo: .init(name: "TestClient", version: "1.0") |
| 138 | + ) |
| 139 | + )) |
| 140 | + |
| 141 | + _ = await transport.waitForSentMessageCount(1, timeout: .seconds(5)) |
| 142 | + |
| 143 | + try await transport.queue( |
| 144 | + notification: InitializedNotification.message(.init()) |
| 145 | + ) |
| 146 | + |
| 147 | + try await Task.sleep(for: .milliseconds(100)) |
| 148 | + |
| 149 | + do { |
| 150 | + _ = try await server.elicit( |
| 151 | + .form(ElicitRequestFormParams( |
| 152 | + message: "Please provide input", |
| 153 | + requestedSchema: ElicitationSchema(properties: [:]) |
| 154 | + )) |
| 155 | + ) |
| 156 | + Issue.record("Expected elicit to throw in stateless mode") |
| 157 | + } catch let error as MCPError { |
| 158 | + let errorDescription = String(describing: error) |
| 159 | + #expect( |
| 160 | + errorDescription.contains("stateless") || errorDescription.contains("server-to-client"), |
| 161 | + "Expected stateless mode error, got: \(errorDescription)" |
| 162 | + ) |
| 163 | + } |
| 164 | + |
| 165 | + await server.stop() |
| 166 | + } |
| 167 | +} |
0 commit comments