package mcp-kit-ppx
All checked builds pass sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>
PPX declarations for MCP servers
Install
dune-project
Dependency
Authors
Maintainers
Sources
ocaml-mcp-v0.2.0.tar.gz
sha256=43c8319a13be91e323ae952a71ab673533fb3295886029eed9d81e5828de5ef7
doc/src/ppx_mcp_kit/ppx_mcp_kit.ml.html
Source file ppx_mcp_kit.ml
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 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 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602open Ppxlib let error ~loc msg = Location.raise_errorf ~loc "%s" msg let string_constant ~loc expr = match expr.pexp_desc with | Pexp_constant (Pconst_string (value, _, _)) -> value | _ -> error ~loc "expected a string literal" let ident ~loc path = let lid = String.split_on_char '.' path |> function | [] -> assert false | head :: rest -> List.fold_left (fun acc item -> Longident.Ldot (acc, item)) (Longident.Lident head) rest in Ast_builder.Default.pexp_ident ~loc { loc; txt = lid } let string_expr ~loc value = Ast_builder.Default.pexp_constant ~loc (Pconst_string (value, loc, None)) let apply ~loc fn args = Ast_builder.Default.pexp_apply ~loc fn args let schema_expr ~loc expr = match expr.pexp_desc with | Pexp_constant (Pconst_string (schema, _, _)) -> apply ~loc (ident ~loc "Mcp_kit.Schema.of_string_exn") [ (Nolabel, string_expr ~loc schema) ] | _ -> expr let empty_schema ~loc = string_expr ~loc {|{"type":"object","properties":{},"required":[]}|} let generated_schema_ident ?(input = false) ~loc expr = let append = function | Longident.Lident name -> Longident.Lident (name ^ "_jsonschema") | Longident.Ldot (prefix, name) -> Longident.Ldot (prefix, name ^ "_jsonschema") | Longident.Lapply _ -> error ~loc "expected a type constructor identifier" in match expr.pexp_desc with | Pexp_ident { txt = Longident.Lident ( "char" | "string" | "bool" | "float" | "int" | "int64" | "unit" | "list" | "array" | "option" ); _; } when input -> error ~loc "mcp.input_type must name a type whose schema root is an object" | Pexp_ident { txt; _ } -> Ast_builder.Default.pexp_ident ~loc { loc; txt = append txt } | _ -> error ~loc "expected a type constructor identifier" let derived_schema_expr ?(input = false) ~loc expr = let raw_schema = apply ~loc (ident ~loc "Ppx_deriving_jsonschema_runtime.json_schema") [ (Nolabel, generated_schema_ident ~input ~loc expr) ] in let schema = apply ~loc (ident ~loc "Mcp_kit.Schema.of_jsonschema") [ (Nolabel, raw_schema) ] in if input then apply ~loc (ident ~loc "Mcp_kit.Schema.require_input_object") [ (Nolabel, schema) ] else schema let attr_expression attr = match attr.attr_payload with | PStr [ { pstr_desc = Pstr_eval (expr, _); _ } ] -> expr | _ -> error ~loc:attr.attr_loc "expected one expression payload" let is_mcp_attr attr = String.starts_with ~prefix:"mcp." attr.attr_name.txt let recognized = [ "mcp.title"; "mcp.description"; "mcp.schema"; "mcp.output_schema"; "mcp.input_type"; "mcp.output_type"; "mcp.input_response_keys"; "mcp.verify_request_state"; "mcp.input_response_kinds"; "mcp.required_client_capabilities"; "mcp.form_response_schemas"; "mcp.form_state_codec"; "mcp.url_state_codec"; "mcp.verify_url_completion"; ] let lookup attrs name = let matching = List.filter (fun attr -> String.equal attr.attr_name.txt name) attrs in match matching with | [] -> None | [ attr ] -> Some (attr_expression attr) | attr :: _ -> error ~loc:attr.attr_loc ("duplicate " ^ name ^ " metadata") let metadata ~loc attrs = List.iter (fun attr -> if is_mcp_attr attr && not (List.mem attr.attr_name.txt recognized) then error ~loc:attr.attr_loc ("unsupported MCP tool metadata: " ^ attr.attr_name.txt)) attrs; let title = Option.map (fun expr -> string_expr ~loc (string_constant ~loc:expr.pexp_loc expr)) (lookup attrs "mcp.title") in let description = match lookup attrs "mcp.description" with | None -> string_expr ~loc "" | Some expr -> string_expr ~loc (string_constant ~loc:expr.pexp_loc expr) in ( title, description, lookup attrs "mcp.schema", lookup attrs "mcp.output_schema", lookup attrs "mcp.input_type", lookup attrs "mcp.output_type", lookup attrs "mcp.input_response_keys", lookup attrs "mcp.verify_request_state", lookup attrs "mcp.input_response_kinds", lookup attrs "mcp.required_client_capabilities", lookup attrs "mcp.form_response_schemas", lookup attrs "mcp.form_state_codec", lookup attrs "mcp.url_state_codec", lookup attrs "mcp.verify_url_completion" ) let drop_metadata attrs = List.filter (fun attr -> not (is_mcp_attr attr)) attrs type kind = Classic | Progress | Interactive let interactive_metadata = [ "mcp.input_response_keys"; "mcp.verify_request_state"; "mcp.input_response_kinds"; "mcp.required_client_capabilities"; "mcp.form_response_schemas"; "mcp.form_state_codec"; "mcp.url_state_codec"; "mcp.verify_url_completion"; ] let constructor = function | Classic -> "Mcp_kit.Tool.make" | Progress -> "Mcp_kit.Tool.make_with_progress" | Interactive -> "Mcp_kit.Tool.make_interactive" let rec decoder_expr ~loc typ = match typ.ptyp_desc with | Ptyp_constr ({ txt = Longident.Lident "string"; _ }, []) -> ident ~loc "Mcp_kit.Tool.Typed.string" | Ptyp_constr ({ txt = Longident.Lident "int"; _ }, []) -> ident ~loc "Mcp_kit.Tool.Typed.int" | Ptyp_constr ({ txt = Longident.Lident "float"; _ }, []) -> ident ~loc "Mcp_kit.Tool.Typed.float" | Ptyp_constr ({ txt = Longident.Lident "bool"; _ }, []) -> ident ~loc "Mcp_kit.Tool.Typed.bool" | Ptyp_constr ({ txt = Longident.Lident "int64"; _ }, []) -> ident ~loc "Mcp_kit.Tool.Typed.int64" | Ptyp_constr ({ txt = Longident.Lident "list"; _ }, [ item ]) -> apply ~loc (ident ~loc "Mcp_kit.Tool.Typed.list") [ (Nolabel, decoder_expr ~loc item) ] | Ptyp_constr ({ txt = Longident.Lident "array"; _ }, [ item ]) -> apply ~loc (ident ~loc "Mcp_kit.Tool.Typed.array") [ (Nolabel, decoder_expr ~loc item) ] | Ptyp_constr ({ txt = Longident.Lident "option"; _ }, [ item ]) -> apply ~loc (ident ~loc "Mcp_kit.Tool.Typed.option") [ (Nolabel, decoder_expr ~loc item) ] | Ptyp_constr ({ txt = Longident.Lident "ref"; _ }, [ item ]) -> apply ~loc (ident ~loc "Mcp_kit.Tool.Typed.ref_") [ (Nolabel, decoder_expr ~loc item) ] | Ptyp_constr ({ txt; _ }, []) -> let append = function | Longident.Lident name -> Longident.Lident (name ^ "_of_yojson") | Longident.Ldot (prefix, name) -> Longident.Ldot (prefix, name ^ "_of_yojson") | Longident.Lapply _ -> error ~loc "unsupported typed tool argument" in Ast_builder.Default.pexp_ident ~loc { loc; txt = append txt } | _ -> error ~loc "unsupported typed tool argument type" let rec schema_for_type ~loc typ = let unit = Ast_builder.Default.eunit ~loc in match typ.ptyp_desc with | Ptyp_constr ({ txt = Longident.Lident "string"; _ }, []) -> apply ~loc (ident ~loc "Mcp_kit.Schema.string") [ (Nolabel, unit) ] | Ptyp_constr ({ txt = Longident.Lident "int"; _ }, []) -> apply ~loc (ident ~loc "Mcp_kit.Schema.integer") [ (Nolabel, unit) ] | Ptyp_constr ({ txt = Longident.Lident "float"; _ }, []) -> apply ~loc (ident ~loc "Mcp_kit.Schema.number") [ (Nolabel, unit) ] | Ptyp_constr ({ txt = Longident.Lident "bool"; _ }, []) -> apply ~loc (ident ~loc "Mcp_kit.Schema.bool") [ (Nolabel, unit) ] | Ptyp_constr ({ txt = Longident.Lident "int64"; _ }, []) -> apply ~loc (ident ~loc "Mcp_kit.Schema.integer") [ (Nolabel, unit) ] | Ptyp_constr ({ txt = Longident.Lident ("list" | "array"); _ }, [ item ]) -> apply ~loc (ident ~loc "Mcp_kit.Schema.array") [ (Nolabel, schema_for_type ~loc item) ] | Ptyp_constr ({ txt = Longident.Lident "option"; _ }, [ item ]) -> apply ~loc (ident ~loc "Mcp_kit.Schema.nullable") [ (Nolabel, schema_for_type ~loc item) ] | Ptyp_constr ({ txt = Longident.Lident "ref"; _ }, [ item ]) -> schema_for_type ~loc item | Ptyp_constr ({ txt; _ }, []) -> derived_schema_expr ~loc (Ast_builder.Default.pexp_ident ~loc { loc; txt }) | _ -> error ~loc "unsupported typed tool argument type" let typed_output_encoder ~loc typ = match typ.ptyp_desc with | Ptyp_constr ({ txt; _ }, []) -> let append = function | Longident.Lident name -> Longident.Lident (name ^ "_to_yojson") | Longident.Ldot (prefix, name) -> Longident.Ldot (prefix, name ^ "_to_yojson") | Longident.Lapply _ -> error ~loc "typed tool output must be a named type" in Ast_builder.Default.pexp_ident ~loc { loc; txt = append txt } | _ -> error ~loc "typed tool output must be a named type" let typed_output_schema ~loc typ = match typ.ptyp_desc with | Ptyp_constr ({ txt; _ }, []) -> derived_schema_expr ~loc (Ast_builder.Default.pexp_ident ~loc { loc; txt }) | _ -> error ~loc "typed tool output must be a named type" let is_domain_type typ = match typ.ptyp_desc with | Ptyp_constr ( { txt = Longident.Lident ( "string" | "int" | "float" | "bool" | "int64" | "list" | "array" | "option" | "ref" ); _; }, _ ) -> false | Ptyp_constr (_, []) -> true | _ -> false let is_optional_type typ = match typ.ptyp_desc with | Ptyp_constr ({ txt = Longident.Lident "option"; _ }, [ _ ]) -> true | _ -> false let typed_handler ~loc ?(whole_input = false) args handler encoder = let applied = List.fold_left (fun fn (name, _) -> apply ~loc fn [ ( Nolabel, Ast_builder.Default.pexp_ident ~loc { loc; txt = Longident.Lident name } ); ]) handler args in let error_case = Ast_builder.Default.case ~lhs: (Ast_builder.Default.ppat_construct ~loc { loc; txt = Longident.Lident "Error" } (Some (Ast_builder.Default.ppat_var ~loc { loc; txt = "message" }))) ~guard:None ~rhs: (Ast_builder.Default.pexp_construct ~loc { loc; txt = Longident.Lident "Error" } (Some (Ast_builder.Default.pexp_ident ~loc { loc; txt = Longident.Lident "message" }))) in let rec decode_with call = function | [] -> apply ~loc (ident ~loc "Result.map") [ ( Nolabel, apply ~loc (ident ~loc "Mcp_kit.Tool.Typed.result") [ (Nolabel, encoder) ] ); (Nolabel, call); ] | (name, typ) :: rest -> let field = let decoder, function_name = match typ.ptyp_desc with | Ptyp_constr ({ txt = Longident.Lident "option"; _ }, [ item ]) -> (decoder_expr ~loc item, "Mcp_kit.Tool.Typed.decode_optional_field") | _ -> (decoder_expr ~loc typ, "Mcp_kit.Tool.Typed.decode_field") in apply ~loc (ident ~loc function_name) [ (Nolabel, string_expr ~loc name); (Nolabel, decoder); ( Nolabel, Ast_builder.Default.pexp_ident ~loc { loc; txt = Longident.Lident "args" } ); ] in Ast_builder.Default.pexp_match ~loc field [ error_case; Ast_builder.Default.case ~lhs: (Ast_builder.Default.ppat_construct ~loc { loc; txt = Longident.Lident "Ok" } (Some (Ast_builder.Default.ppat_var ~loc { loc; txt = name }))) ~guard:None ~rhs:(decode_with call rest); ] in let body = match (whole_input, args) with | true, [ (name, typ) ] -> let decoded = apply ~loc (decoder_expr ~loc typ) [ ( Nolabel, Ast_builder.Default.pexp_ident ~loc { loc; txt = Longident.Lident "args" } ); ] in Ast_builder.Default.pexp_match ~loc decoded [ error_case; Ast_builder.Default.case ~lhs: (Ast_builder.Default.ppat_construct ~loc { loc; txt = Longident.Lident "Ok" } (Some (Ast_builder.Default.ppat_var ~loc { loc; txt = name }))) ~guard:None ~rhs:(decode_with applied []); ] | _ -> decode_with applied args in Ast_builder.Default.pexp_fun ~loc Nolabel None (Ast_builder.Default.ppat_var ~loc { loc; txt = "args" }) body let rec arrow_types typ = match typ.ptyp_desc with | Ptyp_arrow (_, input, output) -> let inputs, result = arrow_types output in (input :: inputs, result) | _ -> ([], typ) let typed_signature ~loc typ = let inputs, result = arrow_types typ in match result.ptyp_desc with | Ptyp_constr ({ txt = Longident.Lident "result"; _ }, [ output; error_type ]) -> ( match error_type.ptyp_desc with | Ptyp_constr ({ txt = Longident.Lident "string"; _ }, []) -> (inputs, output) | _ -> error ~loc "typed tool error type must be string") | _ -> error ~loc "typed tool annotation must return ('output, string) result" let lambda_names ~loc expr = match expr.pexp_desc with | Pexp_function (params, _, _) -> List.map (fun param -> match param.pparam_desc with | Pparam_val (Nolabel, None, { ppat_desc = Ppat_var { txt; _ }; _ }) -> txt | _ -> error ~loc "typed tool parameters must be simple unlabeled variables") params | _ -> error ~loc "typed tool handler must be a lambda" let expand_binding ~loc kind binding = let pattern, annotation = match (binding.pvb_pat.ppat_desc, binding.pvb_constraint) with | Ppat_constraint (pattern, annotation), _ -> (pattern, Some annotation) | _, Some (Pvc_constraint { typ; _ }) -> (binding.pvb_pat, Some typ) | _, Some (Pvc_coercion _) -> error ~loc "typed tool declarations do not support coercions" | _, None -> (binding.pvb_pat, None) in match pattern.ppat_desc with | Ppat_var { txt = name; _ } -> let attrs = binding.pvb_attributes @ pattern.ppat_attributes in (match kind with | Interactive -> () | Classic | Progress -> List.iter (fun attr -> if List.mem attr.attr_name.txt interactive_metadata then error ~loc:attr.attr_loc (attr.attr_name.txt ^ " is only valid on let%mcp.tool.interactive")) attrs); let ( title, supplied_description, schema, output_schema, input_type, output_type, keys, verify, kinds, caps, forms, form_codec, url_codec, verify_url ) = metadata ~loc attrs in let description = if Option.is_some (lookup attrs "mcp.description") then supplied_description else string_expr ~loc name in let input_schema = match (schema, input_type) with | Some _, Some _ -> error ~loc "mcp.schema conflicts with mcp.input_type" | Some value, None -> schema_expr ~loc value | None, Some value -> derived_schema_expr ~input:true ~loc value | None, None -> schema_expr ~loc (empty_schema ~loc) in let output_schema = match (output_schema, output_type) with | Some _, Some _ -> error ~loc "mcp.output_schema conflicts with mcp.output_type" | Some value, None -> Some (schema_expr ~loc value) | None, Some value -> Some (derived_schema_expr ~loc value) | None, None -> None in let interactive = match kind with | Interactive -> let keys = match keys with | Some value -> value | None -> error ~loc "mcp.input_response_keys is required" in let opt label = function | None -> [] | Some value -> [ (Labelled label, value) ] in opt "verify_request_state" verify @ opt "input_response_kinds" kinds @ opt "required_client_capabilities" caps @ opt "form_response_schemas" forms @ opt "form_state_codec" form_codec @ opt "url_state_codec" url_codec @ opt "verify_url_completion" verify_url @ [ (Labelled "input_response_keys", keys) ] | Classic | Progress -> [] in let handler, input_schema, output_schema = match (kind, annotation) with | Classic, Some annotation -> if Option.is_some schema || Option.is_some input_type || Option.is_some output_schema || Option.is_some output_type then error ~loc "typed tool annotations conflict with explicit schema metadata"; let input_types, output_type = typed_signature ~loc annotation in let names = lambda_names ~loc binding.pvb_expr in if List.length names <> List.length input_types then error ~loc "typed tool annotation must match its lambda parameters"; let arguments = List.combine names input_types in let whole_input = match arguments with [ (_, typ) ] -> is_domain_type typ | _ -> false in let input_schema = if whole_input then match arguments with | [ (_, typ) ] -> schema_for_type ~loc typ | _ -> assert false else let properties = Ast_builder.Default.elist ~loc (List.map (fun (field, typ) -> Ast_builder.Default.pexp_tuple ~loc [ string_expr ~loc field; schema_for_type ~loc typ ]) arguments) in let required = Ast_builder.Default.elist ~loc (List.filter_map (fun (name, typ) -> if is_optional_type typ then None else Some (string_expr ~loc name)) arguments) in apply ~loc (ident ~loc "Mcp_kit.Schema.object_") [ (Labelled "properties", properties); (Labelled "required", required); (Nolabel, Ast_builder.Default.eunit ~loc); ] in let output_schema = typed_output_schema ~loc output_type in ( typed_handler ~loc ~whole_input arguments binding.pvb_expr (typed_output_encoder ~loc output_type), input_schema, Some output_schema ) | _, _ -> (binding.pvb_expr, input_schema, output_schema) in let common = (match title with None -> [] | Some value -> [ (Labelled "title", value) ]) @ [ (Labelled "description", description); (Labelled "input_schema", input_schema); ] @ match output_schema with | None -> [] | Some value -> [ (Labelled "output_schema", value) ] in let expr = apply ~loc (ident ~loc (constructor kind)) (common @ interactive @ [ (Nolabel, string_expr ~loc name); (Nolabel, handler) ]) in { binding with pvb_pat = { pattern with ppat_attributes = drop_metadata pattern.ppat_attributes }; pvb_attributes = drop_metadata binding.pvb_attributes; pvb_constraint = None; pvb_expr = expr; } | _ -> error ~loc "MCP tool declarations require a simple value name" let expand_tool_expr ~loc payload = match payload.pexp_desc with | Pexp_tuple [ name; description; schema; handler ] -> let name = string_constant ~loc:name.pexp_loc name in let description = string_constant ~loc:description.pexp_loc description in apply ~loc (ident ~loc "Mcp_kit.Tool.make") [ (Labelled "description", string_expr ~loc description); (Labelled "input_schema", schema_expr ~loc schema); (Nolabel, string_expr ~loc name); (Nolabel, handler); ] | _ -> error ~loc "expected [%mcp.tool (name, description, schema_json, handler)]" let kind_of_name = function | "mcp.tool" -> Some Classic | "mcp.tool.progress" -> Some Progress | "mcp.tool.interactive" -> Some Interactive | _ -> None class mapper = object inherit Ast_traverse.map as super method! expression expr = match expr.pexp_desc with | Pexp_extension ({ txt = "mcp.tool"; loc }, PStr [ { pstr_desc = Pstr_eval (payload, _); _ } ]) -> expand_tool_expr ~loc payload | _ -> super#expression expr method! structure_item item = match item.pstr_desc with | Pstr_extension (({ txt; loc }, PStr [ { pstr_desc = Pstr_value (rec_flag, bindings); _ } ]), _) -> ( match kind_of_name txt with | Some kind -> { item with pstr_desc = Pstr_value (rec_flag, List.map (expand_binding ~loc kind) bindings); } | None -> super#structure_item item) | Pstr_value (rec_flag, bindings) -> let marked binding = List.exists (fun a -> String.equal a.attr_name.txt "mcp.tool") binding.pvb_attributes in if List.exists marked bindings then { item with pstr_desc = Pstr_value ( rec_flag, List.map (fun binding -> if marked binding then expand_binding ~loc:binding.pvb_loc Classic { binding with pvb_attributes = List.filter (fun a -> not (String.equal a.attr_name.txt "mcp.tool")) binding.pvb_attributes; } else binding) bindings ); } else super#structure_item item | _ -> super#structure_item item end let impl structure = (new mapper)#structure structure let () = Driver.register_transformation "mcp_kit_ppx" ~impl
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>