forked from OpenHands/OpenHands
-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathtest_mcp_tool.py
More file actions
212 lines (170 loc) · 6.96 KB
/
Copy pathtest_mcp_tool.py
File metadata and controls
212 lines (170 loc) · 6.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
import unittest
from typing import Any, Dict
from mcp.types import CallToolResult
from openhands.mcp.tool import BaseTool
class TestTool(BaseTool):
"""A simple test implementation of BaseTool for testing purposes."""
name: str = 'test_tool'
description: str = 'A test tool for unit testing'
# Basic schema with primitive types
inputSchema: Dict[str, Any] = {
'type': 'object',
'properties': {
'string_param': {'type': 'string', 'description': 'A string parameter'},
'integer_param': {'type': 'integer', 'description': 'An integer parameter'},
'boolean_param': {'type': 'boolean', 'description': 'A boolean parameter'},
},
'required': ['string_param'],
}
async def execute(self, **kwargs) -> CallToolResult:
"""Test implementation of execute."""
return CallToolResult(content=[], isError=False)
class TestNestedTool(BaseTool):
"""A test tool with nested object schemas using $ref."""
name: str = 'nested_tool'
description: str = 'A tool with nested schema definitions'
# Schema with nested objects using $ref
inputSchema: Dict[str, Any] = {
'type': 'object',
'properties': {
'user': {'$ref': '#/$defs/User'},
'options': {'$ref': '#/$defs/Options'},
},
'required': ['user'],
'$defs': {
'User': {
'type': 'object',
'properties': {
'name': {'type': 'string', 'description': "User's name"},
'age': {'type': 'integer', 'description': "User's age"},
},
'required': ['name'],
'description': 'User information',
},
'Options': {
'type': 'object',
'properties': {
'verbose': {
'type': 'boolean',
'description': 'Enable verbose output',
},
'limit': {
'type': 'integer',
'description': 'Result limit',
'minimum': 1,
'maximum': 100,
},
},
'description': 'Operation options',
},
},
}
async def execute(self, **kwargs) -> CallToolResult:
"""Test implementation of execute."""
return CallToolResult(content=[], isError=False)
class TestEnumTool(BaseTool):
"""A test tool with enum values in schema."""
name: str = 'enum_tool'
description: str = 'A tool with enum values'
# Schema with enum values
inputSchema: Dict[str, Any] = {
'type': 'object',
'properties': {'color': {'$ref': '#/$defs/Color'}},
'required': ['color'],
'$defs': {
'Color': {
'type': 'string',
'enum': ['red', 'green', 'blue'],
'description': 'Color selection',
'default': 'blue',
'title': 'Color Option',
}
},
}
async def execute(self, **kwargs) -> CallToolResult:
"""Test implementation of execute."""
return CallToolResult(content=[], isError=False)
class TestEmptyDescriptionTool(BaseTool):
"""A test tool with an empty description."""
name: str = 'empty_desc_tool'
description: str = '' # Empty description
inputSchema: Dict[str, Any] = {
'type': 'object',
'properties': {'param': {'type': 'string'}},
}
async def execute(self, **kwargs) -> CallToolResult:
"""Test implementation of execute."""
return CallToolResult(content=[], isError=False)
class TestMCPToolParam(unittest.TestCase):
def test_basic_param_conversion(self):
"""Test conversion of a tool with basic primitive types."""
tool = TestTool()
result = tool.to_param()
# Check basic structure
self.assertEqual(result['type'], 'function')
self.assertIn('function', result)
# Check function details
func = result['function']
self.assertEqual(func['name'], 'test_tool_mcp_tool_call')
self.assertEqual(func['description'], 'A test tool for unit testing')
# Check parameters
params = func['parameters']
self.assertEqual(params['type'], 'object')
self.assertIn('properties', params)
self.assertEqual(params['required'], ['string_param'])
# Check properties
props = params['properties']
self.assertEqual(props['string_param']['type'], 'string')
self.assertEqual(props['string_param']['description'], 'A string parameter')
self.assertEqual(props['integer_param']['type'], 'integer')
self.assertEqual(props['boolean_param']['type'], 'boolean')
def test_nested_schema_conversion(self):
"""Test conversion of a tool with nested object schemas."""
tool = TestNestedTool()
result = tool.to_param()
# Check function details
func = result['function']
self.assertEqual(func['name'], 'nested_tool_mcp_tool_call')
# Check parameters
params = func['parameters']
self.assertEqual(params['required'], ['user'])
# Check nested objects
props = params['properties']
# User object
self.assertEqual(props['user']['type'], 'object')
self.assertIn('properties', props['user'])
user_props = props['user']['properties']
self.assertEqual(user_props['name']['type'], 'string')
self.assertEqual(user_props['age']['type'], 'integer')
# Options object
self.assertEqual(props['options']['type'], 'object')
self.assertIn('properties', props['options'])
options_props = props['options']['properties']
self.assertEqual(options_props['verbose']['type'], 'boolean')
self.assertEqual(options_props['limit']['type'], 'integer')
# Note: minimum and maximum properties don't appear to be transferred
# in the current implementation
def test_enum_conversion(self):
"""Test conversion of a tool with enum values."""
tool = TestEnumTool()
result = tool.to_param()
# Check function details
func = result['function']
params = func['parameters']
props = params['properties']
# Check enum properties
self.assertIn('enum', props['color'])
self.assertEqual(props['color']['enum'], ['red', 'green', 'blue'])
self.assertEqual(props['color']['default'], 'blue')
self.assertEqual(props['color']['title'], 'Color Option')
def test_empty_description(self):
"""Test that empty descriptions get a default value."""
tool = TestEmptyDescriptionTool()
result = tool.to_param()
func = result['function']
self.assertEqual(
func['description'],
'Gets information for the empty_desc_tool_mcp_tool_call function',
)
if __name__ == '__main__':
unittest.main()