Skip to content

Commit 1a7a632

Browse files
committed
Fixed optional id on notifications
1 parent 2232808 commit 1a7a632

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

.github/workflows/swift.yml

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
name: Swift
2+
3+
on:
4+
push:
5+
branches: ["main"]
6+
pull_request:
7+
branches: ["main"]
8+
9+
jobs:
10+
build:
11+
runs-on: macos-latest
12+
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup Swift 6.0
17+
uses: swift-actions/setup-swift@v2.2.0
18+
with:
19+
swift-version: "6.0"
20+
21+
- name: Verify Swift version
22+
run: swift --version
23+
24+
- name: Build & Test
25+
run: swift test
26+
27+
build-linux:
28+
runs-on: ubuntu-latest
29+
30+
steps:
31+
- uses: actions/checkout@v4
32+
33+
- name: Setup Swift 6.0
34+
uses: swift-actions/setup-swift@v2.2.0
35+
with:
36+
swift-version: "6.0"
37+
38+
- name: Verify Swift version
39+
run: swift --version
40+
41+
- name: Build & Test
42+
run: swift test

Sources/SwiftMCP/Models/JSONRPC/JSONRPCRequest.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ import AnyCodable
44
/// JSON-RPC Request structure used for communication with the MCP server
55
public struct JSONRPCRequest: Codable {
66
public let jsonrpc: String
7-
public let id: Int
7+
public let id: Int?
88
public let method: String
99
public let params: [String: AnyCodable]?
1010

11-
public init(jsonrpc: String, id: Int, method: String, params: [String: AnyCodable]? = nil) {
11+
public init(jsonrpc: String, id: Int?, method: String, params: [String: AnyCodable]? = nil) {
1212
self.jsonrpc = jsonrpc
1313
self.id = id
1414
self.method = method
@@ -42,4 +42,4 @@ public struct JSONRPCRequest: Codable {
4242

4343
return current
4444
}
45-
}
45+
}

Sources/SwiftMCP/Models/JSONRPC/JSONRPCResponse.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ import Foundation
55
/// JSON-RPC Response structures
66
public struct JSONRPCResponse: Codable {
77
public var jsonrpc: String = "2.0"
8-
public let id: Int
8+
public let id: Int?
99
public let result: ResponseResult
1010

11-
public init(jsonrpc: String = "2.0", id: Int, result: ResponseResult) {
11+
public init(jsonrpc: String = "2.0", id: Int?, result: ResponseResult) {
1212
self.jsonrpc = jsonrpc
1313
self.id = id
1414
self.result = result
@@ -53,4 +53,4 @@ public struct JSONRPCResponse: Codable {
5353
}
5454
}
5555
}
56-
}
56+
}

Sources/SwiftMCP/Protocols/MCPServer.swift

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ public extension MCPServer {
2929
// Prepare the response based on the method
3030
switch request.method {
3131
case "initialize":
32-
return createInitializeResponse(id: request.id)
32+
33+
return createInitializeResponse(id: request.id ?? 0)
3334

3435
case "notifications/initialized":
3536
return nil
3637

3738
case "tools/list":
38-
return createToolsResponse(id: request.id)
39+
return createToolsResponse(id: request.id ?? 0)
3940

4041
case "tools/call":
4142
return handleToolCall(request)
@@ -105,7 +106,7 @@ public extension MCPServer {
105106
}
106107

107108
// Create and encode the response
108-
return ToolCallResponse(id: request.id, text: responseText, isError: isError)
109+
return ToolCallResponse(id: request.id ?? 0, text: responseText, isError: isError)
109110
}
110111

111112
/// Function to log a message to stderr

0 commit comments

Comments
 (0)