Skip to content

Commit 1698638

Browse files
committed
Checkpoint
1 parent c441378 commit 1698638

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

Demos/SwiftMCPDemo/Calculator.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,16 @@ import SwiftMCP
66
*/
77
@MCPServer(name: "SwiftMCP Demo")
88
actor Calculator {
9+
10+
/// Sends an email
11+
/// - Parameter subject: The subject of the email
12+
/// - Returns Some confirmation
13+
@MCPTool
14+
func searchEmailSubject(for subject: String) async throws -> String
15+
{
16+
return "Subject is \(subject)"
17+
}
18+
919
/// Adds two integers and returns their sum
1020
/// - Parameter a: First number to add
1121
/// - Parameter b: Second number to add

Sources/SwiftMCP/Models/Tools/MCPToolParameterInfo.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ public struct MCPToolParameterInfo: Sendable {
1212
/// The name of the parameter
1313
public let name: String
1414

15+
/// The label of the parameter (e.g., "for" in "for subject: String")
16+
public let label: String
17+
1518
/// The type of the parameter
1619
public let type: String
1720

@@ -26,12 +29,14 @@ public struct MCPToolParameterInfo: Sendable {
2629

2730
- Parameters:
2831
- name: The name of the parameter
32+
- label: The label of the parameter (e.g., "for" in "for subject: String")
2933
- type: The type of the parameter
3034
- description: An optional description of the parameter
3135
- defaultValue: An optional default value for the parameter
3236
*/
33-
public init(name: String, type: String, description: String? = nil, defaultValue: String? = nil) {
37+
public init(name: String, label: String, type: String, description: String? = nil, defaultValue: String? = nil) {
3438
self.name = name
39+
self.label = label
3540
self.type = type
3641
self.description = description
3742
self.defaultValue = defaultValue

Sources/SwiftMCPMacros/MCPToolMacro.swift

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public struct MCPToolMacro: PeerMacro {
129129

130130
// Extract parameter information
131131
var parameterString = ""
132-
var parameterInfos: [(name: String, type: String, defaultValue: String?)] = []
132+
var parameterInfos: [(name: String, label: String, type: String, defaultValue: String?)] = []
133133

134134
// Extract return type information from the syntax tree
135135
let returnTypeString: String
@@ -143,7 +143,9 @@ public struct MCPToolMacro: PeerMacro {
143143
}
144144

145145
for param in funcDecl.signature.parameterClause.parameters {
146-
let paramName = param.firstName.text
146+
// Get the parameter name (secondName) and label (firstName)
147+
let paramName = param.secondName?.text ?? param.firstName.text
148+
let paramLabel = param.firstName.text
147149
let paramType = param.type.description.trimmingCharacters(in: .whitespacesAndNewlines)
148150

149151
// Store parameter info for wrapper function generation
@@ -225,10 +227,10 @@ public struct MCPToolMacro: PeerMacro {
225227
parameterString += ", "
226228
}
227229

228-
parameterString += "MCPToolParameterInfo(name: \"\(paramName)\", type: \"\(paramType)\", description: \(paramDescription), defaultValue: \(defaultValue))"
230+
parameterString += "MCPToolParameterInfo(name: \"\(paramName)\", label: \"\(paramLabel)\", type: \"\(paramType)\", description: \(paramDescription), defaultValue: \(defaultValue))"
229231

230232
// Store parameter info for wrapper function generation
231-
parameterInfos.append((name: paramName, type: paramType, defaultValue: defaultValueStr))
233+
parameterInfos.append((name: paramName, label: paramLabel, type: paramType, defaultValue: defaultValueStr))
232234
}
233235

234236
// Create a registration statement using string interpolation for simplicity
@@ -256,6 +258,7 @@ public struct MCPToolMacro: PeerMacro {
256258
// Add parameter extraction code
257259
for param in parameterInfos {
258260
let paramName = param.name
261+
let paramLabel = param.label
259262
let paramType = param.type
260263

261264
// If it has a default value, use conditional binding
@@ -322,7 +325,7 @@ public struct MCPToolMacro: PeerMacro {
322325
} else {
323326
actualType = "nil"
324327
}
325-
throw MCPToolError.invalidArgumentType(parameterName: "\(paramName)", expectedType: "\(paramType)", actualType: actualType)
328+
throw MCPToolError.invalidArgumentType(parameterName: "\(paramLabel) \(paramName)", expectedType: "\(paramType)", actualType: actualType)
326329
}
327330
"""
328331
} else if param.type == "Int" {
@@ -343,7 +346,7 @@ public struct MCPToolMacro: PeerMacro {
343346
} else {
344347
actualType = "nil"
345348
}
346-
throw MCPToolError.invalidArgumentType(parameterName: "\(paramName)", expectedType: "\(paramType)", actualType: actualType)
349+
throw MCPToolError.invalidArgumentType(parameterName: "\(paramLabel) \(paramName)", expectedType: "\(paramType)", actualType: actualType)
347350
}
348351
"""
349352
} else {
@@ -357,15 +360,15 @@ public struct MCPToolMacro: PeerMacro {
357360
} else {
358361
actualType = "nil"
359362
}
360-
throw MCPToolError.invalidArgumentType(parameterName: "\(paramName)", expectedType: "\(paramType)", actualType: actualType)
363+
throw MCPToolError.invalidArgumentType(parameterName: "\(paramLabel) \(paramName)", expectedType: "\(paramType)", actualType: actualType)
361364
}
362365
"""
363366
}
364367
}
365368
}
366369

367370
// Add the function call
368-
let parameterList = parameterInfos.map { "\($0.name): \($0.name)" }.joined(separator: ", ")
371+
let parameterList = parameterInfos.map { "\($0.label): \($0.name)" }.joined(separator: ", ")
369372
let isThrowing = funcDecl.signature.effectSpecifiers?.throwsClause?.throwsSpecifier != nil
370373
let isAsync = funcDecl.signature.effectSpecifiers?.asyncSpecifier != nil
371374

0 commit comments

Comments
 (0)