func_metadata
StrictJsonSchema
Bases: GenerateJsonSchema
A JSON schema generator that raises exceptions instead of emitting warnings.
This is used to detect non-serializable types during schema generation.
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
32 33 34 35 36 37 38 39 40 | |
ArgModelBase
Bases: BaseModel
A model representing the arguments to a function.
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | |
model_dump_one_level
Return a dict of the model's fields, one level deep.
That is, sub-models etc are not dumped - they are kept as Pydantic models.
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
46 47 48 49 50 51 52 53 54 55 56 57 | |
FuncMetadata
Bases: BaseModel
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
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 | |
call_fn_with_arg_validation
async
call_fn_with_arg_validation(
fn: Callable[..., Any | Awaitable[Any]],
fn_is_async: bool,
arguments_to_validate: dict[str, Any],
arguments_to_pass_directly: dict[str, Any] | None,
) -> Any
Call the given function with arguments validated and injected.
Arguments are first attempted to be parsed from JSON, then validated against the argument model, before being passed to the function.
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | |
convert_result
Convert a function call result to the format for the lowlevel tool call handler.
- If output_model is None, return the unstructured content directly.
- If output_model is not None, convert the result to structured output format (dict[str, Any]) and return both unstructured and structured content.
Note: we return unstructured content here even though the lowlevel server tool call handler provides generic backwards compatibility serialization of structured content. This is for MCPServer backwards compatibility: we need to retain MCPServer's ad hoc conversion logic for constructing unstructured output from function return values, whereas the lowlevel server simply serializes the structured output.
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
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 | |
pre_parse_json
Pre-parse data from JSON.
Return a dict with the same keys as input but with values parsed from JSON if appropriate.
This is to handle cases like ["a", "b", "c"] being passed in as JSON inside
a string rather than an actual list. Claude Desktop is prone to this - in fact
it seems incapable of NOT doing this. For sub-models, it tends to pass
dicts (JSON objects) as JSON strings, which can be pre-parsed here.
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
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 | |
func_metadata
func_metadata(
func: Callable[..., Any],
skip_names: Sequence[str] = (),
structured_output: bool | None = None,
) -> FuncMetadata
Given a function, return metadata including a Pydantic model representing its signature.
The use case for this is
meta = func_metadata(func)
validated_args = meta.arg_model.model_validate(some_raw_data_dict)
return func(**validated_args.model_dump_one_level())
critically it also provides a pre-parse helper to attempt to parse things from JSON.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The function to convert to a Pydantic model |
required |
skip_names
|
Sequence[str]
|
A list of parameter names to skip. These will not be included in the model. |
()
|
structured_output
|
bool | None
|
Controls whether the tool's output is structured or unstructured - If None, auto-detects based on the function's return type annotation - If True, creates a structured tool (return type annotation permitting) - If False, unconditionally creates an unstructured tool If structured, creates a Pydantic model for the function's result based on its annotation. Supports various return types: - BaseModel subclasses (used directly) - Primitive types (str, int, float, bool, bytes, None) - wrapped in a model with a 'result' field - TypedDict - converted to a Pydantic model with same fields - Dataclasses and other annotated classes - converted to Pydantic models - Generic types (list, dict, Union, etc.) - wrapped in a model with a 'result' field |
None
|
Returns:
| Type | Description |
|---|---|
FuncMetadata
|
A FuncMetadata object containing: |
FuncMetadata
|
|
FuncMetadata
|
|
FuncMetadata
|
|
Source code in src/mcp/server/mcpserver/utilities/func_metadata.py
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 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |