Skip to content

Commit 3fa49a8

Browse files
authored
new feature: add tool annnotation (#158)
1 parent 7335e3a commit 3fa49a8

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed

mcp/tools.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ type Tool struct {
7575
InputSchema ToolInputSchema `json:"inputSchema"`
7676
// Alternative to InputSchema - allows arbitrary JSON Schema to be provided
7777
RawInputSchema json.RawMessage `json:"-"` // Hide this from JSON marshaling
78+
// Optional properties describing tool behavior
79+
Annotations ToolAnnotation `json:"annotations"`
7880
}
7981

8082
// MarshalJSON implements the json.Marshaler interface for Tool.
@@ -109,6 +111,19 @@ type ToolInputSchema struct {
109111
Required []string `json:"required,omitempty"`
110112
}
111113

114+
type ToolAnnotation struct {
115+
// Human-readable title for the tool
116+
Title string `json:"title,omitempty"`
117+
// If true, the tool does not modify its environment
118+
ReadOnlyHint bool `json:"readOnlyHint,omitempty"`
119+
// If true, the tool may perform destructive updates
120+
DestructiveHint bool `json:"destructiveHint,omitempty"`
121+
// If true, repeated calls with same args have no additional effect
122+
IdempotentHint bool `json:"idempotentHint,omitempty"`
123+
// If true, tool interacts with external entities
124+
OpenWorldHint bool `json:"openWorldHint,omitempty"`
125+
}
126+
112127
// ToolOption is a function that configures a Tool.
113128
// It provides a flexible way to set various properties of a Tool using the functional options pattern.
114129
type ToolOption func(*Tool)
@@ -132,6 +147,13 @@ func NewTool(name string, opts ...ToolOption) Tool {
132147
Properties: make(map[string]interface{}),
133148
Required: nil, // Will be omitted from JSON if empty
134149
},
150+
Annotations: ToolAnnotation{
151+
Title: "",
152+
ReadOnlyHint: false,
153+
DestructiveHint: true,
154+
IdempotentHint: false,
155+
OpenWorldHint: true,
156+
},
135157
}
136158

137159
for _, opt := range opts {
@@ -166,6 +188,12 @@ func WithDescription(description string) ToolOption {
166188
}
167189
}
168190

191+
func WithToolAnnotation(annotation ToolAnnotation) ToolOption {
192+
return func(t *Tool) {
193+
t.Annotations = annotation
194+
}
195+
}
196+
169197
//
170198
// Common Property Options
171199
//

server/server_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -800,6 +800,13 @@ func TestMCPServer_HandleUndefinedHandlers(t *testing.T) {
800800
Type: "object",
801801
Properties: map[string]interface{}{},
802802
},
803+
Annotations: mcp.ToolAnnotation{
804+
Title: "test-tool",
805+
ReadOnlyHint: true,
806+
DestructiveHint: false,
807+
IdempotentHint: false,
808+
OpenWorldHint: false,
809+
},
803810
}, func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) {
804811
return &mcp.CallToolResult{}, nil
805812
})

0 commit comments

Comments
 (0)