Skip to content

Commit 0bc42d8

Browse files
committed
Added doc about supported types
1 parent ecb4eb9 commit 0bc42d8

File tree

2 files changed

+136
-1
lines changed

2 files changed

+136
-1
lines changed
Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Supported Types in MCP Tools
2+
3+
Learn about the parameter types and function signatures supported by MCP tools.
4+
5+
## Overview
6+
7+
When creating MCP tools using the `@MCPTool` macro, you can use various parameter types and function signatures. This article covers all supported types and shows examples of how to use them.
8+
9+
## Parameter Types
10+
11+
MCP tools support the following parameter types:
12+
13+
### Basic Types
14+
- `Int`
15+
- `Double`
16+
- `Float`
17+
- `String`
18+
- `Bool`
19+
20+
### Array Types
21+
- `[Int]`
22+
- `[Double]`
23+
- `[Float]`
24+
- `[String]`
25+
- `[Bool]`
26+
27+
### Enum Types
28+
Enums without associated values that conform to `CaseIterable` and `Sendable` are supported. By default, the case labels are used as strings when the enum is serialized. You can customize the string representation by implementing `CustomStringConvertible`:
29+
30+
```swift
31+
// Default behavior uses case labels
32+
enum SearchOption: CaseIterable, Sendable {
33+
case all // Will be serialized as "all"
34+
case unread // Will be serialized as "unread"
35+
case flagged // Will be serialized as "flagged"
36+
}
37+
38+
// Custom string representation using CustomStringConvertible
39+
enum FilterOption: CaseIterable, Sendable, CustomStringConvertible {
40+
case newest
41+
case oldest
42+
case popular
43+
44+
var description: String {
45+
switch self {
46+
case .newest: return "SORT_NEW"
47+
case .oldest: return "SORT_OLD"
48+
case .popular: return "SORT_POPULAR"
49+
}
50+
}
51+
}
52+
53+
// Raw values are ignored for serialization
54+
enum Priority: String, CaseIterable, Sendable {
55+
case high = "H" // Will be serialized as "high"
56+
case medium = "M" // Will be serialized as "medium"
57+
case low = "L" // Will be serialized as "low"
58+
}
59+
```
60+
61+
## Function Signatures
62+
63+
MCP tools support various function signatures:
64+
65+
### Basic Functions
66+
```swift
67+
@MCPTool
68+
func add(a: Int, b: Int) -> Int {
69+
return a + b
70+
}
71+
```
72+
73+
### Async Functions
74+
```swift
75+
@MCPTool
76+
func fetchData(query: String) async -> String {
77+
// ... async implementation
78+
}
79+
```
80+
81+
### Throwing Functions
82+
```swift
83+
@MCPTool
84+
func divide(numerator: Double, denominator: Double) throws -> Double {
85+
guard denominator != 0 else {
86+
throw MathError.divisionByZero
87+
}
88+
return numerator / denominator
89+
}
90+
```
91+
92+
### Async Throwing Functions
93+
```swift
94+
@MCPTool
95+
func processData(input: String) async throws -> String {
96+
// ... async throwing implementation
97+
}
98+
```
99+
100+
## Return Types
101+
102+
The return type of an MCP tool must conform to both `Sendable` and `Codable`. This includes:
103+
104+
- All basic types (`Int`, `Double`, `Float`, `String`, `Bool`)
105+
- Arrays of basic types
106+
- Custom types that conform to both protocols
107+
- `Void` (for functions that don't return a value)
108+
109+
## Default Values
110+
111+
Parameters can have default values:
112+
113+
```swift
114+
@MCPTool
115+
func greet(name: String = "World", times: Int = 1) -> String {
116+
return String(repeating: "Hello, \(name)! ", count: times)
117+
}
118+
```
119+
120+
## Tips
121+
122+
- For enum parameters, ensure they conform to `CaseIterable` and `Sendable`
123+
- By default, case labels are used for serialization
124+
- You can customize enum string representation by implementing `CustomStringConvertible`
125+
- Raw values are ignored for serialization purposes
126+
- For custom types, ensure they conform to both `Sendable` and `Codable`
127+
- Default values are supported for all parameter types
128+
- Function parameters and return types must be `Sendable` to ensure thread safety
129+
130+
## Topics
131+
132+
### Related Articles
133+
- ``MCPServer``
134+
- ``MCPTool``

Sources/SwiftMCP/SwiftMCP.docc/SwiftMCP.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ Start with <doc:GettingStarted> to create your first MCP server, then explore <d
3535

3636
## Topics
3737

38-
### Essentials
38+
### Getting Started
3939

4040
- <doc:GettingStarted>
4141
- <doc:CoreConcepts>
4242
- <doc:SwiftMCPTutorials>
43+
- <doc:SupportedTypes>
4344

4445
### Macros
4546

0 commit comments

Comments
 (0)