this repo has no description
6
fork

Configure Feed

Select the types of activity you want to include in your feed.

Add Resource, ResourceTemplate, and Completion modules

- Added Resource module to represent server resources
- Added ResourceTemplate for URI templates
- Added ResourceReference and PromptReference for references
- Added Completion support with Request and Result modules
- Added helper functions for creating completion requests and responses

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

+441 -2
+334 -1
lib/mcp.ml
··· 445 445 | j -> raise (Json.Of_json ("Expected object for CallToolResult", j)) 446 446 end 447 447 448 + (** Resource definition *) 449 + module Resource = struct 450 + type t = { 451 + name: string; 452 + uri: string; 453 + description: string option; 454 + mime_type: string option; 455 + size: int option; 456 + annotations: Annotated.annotation option; 457 + } 458 + 459 + let yojson_of_t { name; uri; description; mime_type; size; annotations } = 460 + let assoc = [ 461 + ("name", `String name); 462 + ("uri", `String uri); 463 + ] in 464 + let assoc = match description with 465 + | Some desc -> ("description", `String desc) :: assoc 466 + | None -> assoc 467 + in 468 + let assoc = match mime_type with 469 + | Some mime -> ("mimeType", `String mime) :: assoc 470 + | None -> assoc 471 + in 472 + let assoc = match size with 473 + | Some s -> ("size", `Int s) :: assoc 474 + | None -> assoc 475 + in 476 + let assoc = match annotations with 477 + | Some ann -> ("annotations", Annotated.yojson_of_annotation ann) :: assoc 478 + | None -> assoc 479 + in 480 + `Assoc assoc 481 + 482 + let t_of_yojson = function 483 + | `Assoc fields -> 484 + let name = match List.assoc_opt "name" fields with 485 + | Some (`String s) -> s 486 + | _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields)) 487 + in 488 + let uri = match List.assoc_opt "uri" fields with 489 + | Some (`String s) -> s 490 + | _ -> raise (Json.Of_json ("Missing or invalid 'uri' field", `Assoc fields)) 491 + in 492 + let description = match List.assoc_opt "description" fields with 493 + | Some (`String s) -> Some s 494 + | _ -> None 495 + in 496 + let mime_type = match List.assoc_opt "mimeType" fields with 497 + | Some (`String s) -> Some s 498 + | _ -> None 499 + in 500 + let size = match List.assoc_opt "size" fields with 501 + | Some (`Int s) -> Some s 502 + | _ -> None 503 + in 504 + let annotations = match List.assoc_opt "annotations" fields with 505 + | Some json -> Some (Annotated.annotation_of_yojson json) 506 + | _ -> None 507 + in 508 + { name; uri; description; mime_type; size; annotations } 509 + | j -> raise (Json.Of_json ("Expected object for Resource", j)) 510 + end 511 + 512 + (** Resource Template definition *) 513 + module ResourceTemplate = struct 514 + type t = { 515 + name: string; 516 + uri_template: string; 517 + description: string option; 518 + mime_type: string option; 519 + annotations: Annotated.annotation option; 520 + } 521 + 522 + let yojson_of_t { name; uri_template; description; mime_type; annotations } = 523 + let assoc = [ 524 + ("name", `String name); 525 + ("uriTemplate", `String uri_template); 526 + ] in 527 + let assoc = match description with 528 + | Some desc -> ("description", `String desc) :: assoc 529 + | None -> assoc 530 + in 531 + let assoc = match mime_type with 532 + | Some mime -> ("mimeType", `String mime) :: assoc 533 + | None -> assoc 534 + in 535 + let assoc = match annotations with 536 + | Some ann -> ("annotations", Annotated.yojson_of_annotation ann) :: assoc 537 + | None -> assoc 538 + in 539 + `Assoc assoc 540 + 541 + let t_of_yojson = function 542 + | `Assoc fields -> 543 + let name = match List.assoc_opt "name" fields with 544 + | Some (`String s) -> s 545 + | _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields)) 546 + in 547 + let uri_template = match List.assoc_opt "uriTemplate" fields with 548 + | Some (`String s) -> s 549 + | _ -> raise (Json.Of_json ("Missing or invalid 'uriTemplate' field", `Assoc fields)) 550 + in 551 + let description = match List.assoc_opt "description" fields with 552 + | Some (`String s) -> Some s 553 + | _ -> None 554 + in 555 + let mime_type = match List.assoc_opt "mimeType" fields with 556 + | Some (`String s) -> Some s 557 + | _ -> None 558 + in 559 + let annotations = match List.assoc_opt "annotations" fields with 560 + | Some json -> Some (Annotated.annotation_of_yojson json) 561 + | _ -> None 562 + in 563 + { name; uri_template; description; mime_type; annotations } 564 + | j -> raise (Json.Of_json ("Expected object for ResourceTemplate", j)) 565 + end 566 + 567 + (** Resource Reference *) 568 + module ResourceReference = struct 569 + type t = { 570 + uri: string; 571 + } 572 + 573 + let yojson_of_t { uri } = 574 + `Assoc [ 575 + ("type", `String "ref/resource"); 576 + ("uri", `String uri); 577 + ] 578 + 579 + let t_of_yojson = function 580 + | `Assoc fields -> 581 + let _ = match List.assoc_opt "type" fields with 582 + | Some (`String "ref/resource") -> () 583 + | _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields)) 584 + in 585 + let uri = match List.assoc_opt "uri" fields with 586 + | Some (`String s) -> s 587 + | _ -> raise (Json.Of_json ("Missing or invalid 'uri' field", `Assoc fields)) 588 + in 589 + { uri } 590 + | j -> raise (Json.Of_json ("Expected object for ResourceReference", j)) 591 + end 592 + 593 + (** Prompt Reference *) 594 + module PromptReference = struct 595 + type t = { 596 + name: string; 597 + } 598 + 599 + let yojson_of_t { name } = 600 + `Assoc [ 601 + ("type", `String "ref/prompt"); 602 + ("name", `String name); 603 + ] 604 + 605 + let t_of_yojson = function 606 + | `Assoc fields -> 607 + let _ = match List.assoc_opt "type" fields with 608 + | Some (`String "ref/prompt") -> () 609 + | _ -> raise (Json.Of_json ("Missing or invalid 'type' field", `Assoc fields)) 610 + in 611 + let name = match List.assoc_opt "name" fields with 612 + | Some (`String s) -> s 613 + | _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields)) 614 + in 615 + { name } 616 + | j -> raise (Json.Of_json ("Expected object for PromptReference", j)) 617 + end 618 + 619 + (** Completion support *) 620 + module Completion = struct 621 + 622 + module Argument = struct 623 + type t = { 624 + name: string; 625 + value: string; 626 + } 627 + 628 + let yojson_of_t { name; value } = 629 + `Assoc [ 630 + ("name", `String name); 631 + ("value", `String value); 632 + ] 633 + 634 + let t_of_yojson = function 635 + | `Assoc fields -> 636 + let name = match List.assoc_opt "name" fields with 637 + | Some (`String s) -> s 638 + | _ -> raise (Json.Of_json ("Missing or invalid 'name' field", `Assoc fields)) 639 + in 640 + let value = match List.assoc_opt "value" fields with 641 + | Some (`String s) -> s 642 + | _ -> raise (Json.Of_json ("Missing or invalid 'value' field", `Assoc fields)) 643 + in 644 + { name; value } 645 + | j -> raise (Json.Of_json ("Expected object for Completion.Argument", j)) 646 + end 647 + 648 + module Request = struct 649 + type reference = [ `Prompt of PromptReference.t | `Resource of ResourceReference.t ] 650 + 651 + type t = { 652 + argument: Argument.t; 653 + ref: reference; 654 + } 655 + 656 + let yojson_of_reference = function 657 + | `Prompt p -> PromptReference.yojson_of_t p 658 + | `Resource r -> ResourceReference.yojson_of_t r 659 + 660 + let reference_of_yojson = function 661 + | `Assoc fields -> 662 + (match List.assoc_opt "type" fields with 663 + | Some (`String "ref/prompt") -> `Prompt (PromptReference.t_of_yojson (`Assoc fields)) 664 + | Some (`String "ref/resource") -> `Resource (ResourceReference.t_of_yojson (`Assoc fields)) 665 + | _ -> raise (Json.Of_json ("Invalid or missing reference type", `Assoc fields))) 666 + | j -> raise (Json.Of_json ("Expected object for reference", j)) 667 + 668 + let yojson_of_t { argument; ref } = 669 + `Assoc [ 670 + ("argument", Argument.yojson_of_t argument); 671 + ("ref", yojson_of_reference ref); 672 + ] 673 + 674 + let t_of_yojson = function 675 + | `Assoc fields -> 676 + let argument = match List.assoc_opt "argument" fields with 677 + | Some json -> Argument.t_of_yojson json 678 + | _ -> raise (Json.Of_json ("Missing argument field", `Assoc fields)) 679 + in 680 + let ref = match List.assoc_opt "ref" fields with 681 + | Some json -> reference_of_yojson json 682 + | _ -> raise (Json.Of_json ("Missing ref field", `Assoc fields)) 683 + in 684 + { argument; ref } 685 + | j -> raise (Json.Of_json ("Expected object for Completion.Request", j)) 686 + 687 + let create ~argument ~ref = 688 + { argument; ref } 689 + 690 + let to_params t = 691 + yojson_of_t t 692 + end 693 + 694 + module Result = struct 695 + type completion = { 696 + values: string list; 697 + has_more: bool option; 698 + total: int option; 699 + } 700 + 701 + type t = { 702 + completion: completion; 703 + meta: Json.t option; 704 + } 705 + 706 + let yojson_of_completion { values; has_more; total } = 707 + let assoc = [ 708 + ("values", `List (List.map (fun s -> `String s) values)); 709 + ] in 710 + let assoc = match has_more with 711 + | Some b -> ("hasMore", `Bool b) :: assoc 712 + | None -> assoc 713 + in 714 + let assoc = match total with 715 + | Some n -> ("total", `Int n) :: assoc 716 + | None -> assoc 717 + in 718 + `Assoc assoc 719 + 720 + let completion_of_yojson = function 721 + | `Assoc fields -> 722 + let values = match List.assoc_opt "values" fields with 723 + | Some (`List items) -> 724 + List.map (function 725 + | `String s -> s 726 + | _ -> raise (Json.Of_json ("Expected string in values array", `List items)) 727 + ) items 728 + | _ -> raise (Json.Of_json ("Missing or invalid 'values' field", `Assoc fields)) 729 + in 730 + let has_more = match List.assoc_opt "hasMore" fields with 731 + | Some (`Bool b) -> Some b 732 + | None -> None 733 + | _ -> raise (Json.Of_json ("Invalid 'hasMore' field", `Assoc fields)) 734 + in 735 + let total = match List.assoc_opt "total" fields with 736 + | Some (`Int n) -> Some n 737 + | None -> None 738 + | _ -> raise (Json.Of_json ("Invalid 'total' field", `Assoc fields)) 739 + in 740 + { values; has_more; total } 741 + | j -> raise (Json.Of_json ("Expected object for completion", j)) 742 + 743 + let yojson_of_t { completion; meta } = 744 + let assoc = [ 745 + ("completion", yojson_of_completion completion); 746 + ] in 747 + let assoc = match meta with 748 + | Some meta_json -> ("_meta", meta_json) :: assoc 749 + | None -> assoc 750 + in 751 + `Assoc assoc 752 + 753 + let t_of_yojson = function 754 + | `Assoc fields -> 755 + let completion = match List.assoc_opt "completion" fields with 756 + | Some json -> completion_of_yojson json 757 + | _ -> raise (Json.Of_json ("Missing completion field", `Assoc fields)) 758 + in 759 + let meta = List.assoc_opt "_meta" fields in 760 + { completion; meta } 761 + | j -> raise (Json.Of_json ("Expected object for Completion.Result", j)) 762 + 763 + let create ~completion ?meta () = 764 + { completion; meta } 765 + 766 + let to_result t = 767 + yojson_of_t t 768 + end 769 + end 770 + 448 771 (* Message types *) 449 772 450 773 module PromptMessage = struct ··· 871 1194 let create_notification = JSONRPCMessage.create_notification 872 1195 let create_request = JSONRPCMessage.create_request 873 1196 let create_response = JSONRPCMessage.create_response 874 - let create_error = JSONRPCMessage.create_error 1197 + let create_error = JSONRPCMessage.create_error 1198 + 1199 + (* Helper functions *) 1200 + let create_completion_request ~id ~argument ~ref = 1201 + let params = Completion.Request.to_params { argument; ref } in 1202 + create_request ~id ~method_:"completion/complete" ~params:(Some params) () 1203 + 1204 + let create_completion_response ~id ~values ?(has_more=None) ?(total=None) ?(meta=None) () = 1205 + let completion = { Completion.Result.values; has_more; total } in 1206 + let result = Completion.Result.to_result { completion; meta } in 1207 + create_response ~id ~result
+107 -1
lib/mcp.mli
··· 314 314 val t_of_yojson : Json.t -> t 315 315 end 316 316 317 + (** Resource definition *) 318 + module Resource : sig 319 + type t = { 320 + name: string; 321 + uri: string; 322 + description: string option; 323 + mime_type: string option; 324 + size: int option; 325 + annotations: Annotated.annotation option; 326 + } 327 + 328 + val yojson_of_t : t -> Json.t 329 + val t_of_yojson : Json.t -> t 330 + end 331 + 332 + (** Resource Template definition *) 333 + module ResourceTemplate : sig 334 + type t = { 335 + name: string; 336 + uri_template: string; 337 + description: string option; 338 + mime_type: string option; 339 + annotations: Annotated.annotation option; 340 + } 341 + 342 + val yojson_of_t : t -> Json.t 343 + val t_of_yojson : Json.t -> t 344 + end 345 + 346 + (** Resource Reference *) 347 + module ResourceReference : sig 348 + type t = { 349 + uri: string; 350 + } 351 + 352 + val yojson_of_t : t -> Json.t 353 + val t_of_yojson : Json.t -> t 354 + end 355 + 356 + (** Prompt Reference *) 357 + module PromptReference : sig 358 + type t = { 359 + name: string; 360 + } 361 + 362 + val yojson_of_t : t -> Json.t 363 + val t_of_yojson : Json.t -> t 364 + end 365 + 366 + (** Completion support *) 367 + module Completion : sig 368 + module Argument : sig 369 + type t = { 370 + name: string; 371 + value: string; 372 + } 373 + 374 + val yojson_of_t : t -> Json.t 375 + val t_of_yojson : Json.t -> t 376 + end 377 + 378 + module Request : sig 379 + type reference = [ `Prompt of PromptReference.t | `Resource of ResourceReference.t ] 380 + 381 + type t = { 382 + argument: Argument.t; 383 + ref: reference; 384 + } 385 + 386 + val yojson_of_t : t -> Json.t 387 + val t_of_yojson : Json.t -> t 388 + 389 + val yojson_of_reference : reference -> Json.t 390 + val reference_of_yojson : Json.t -> reference 391 + 392 + val create : argument:Argument.t -> ref:reference -> t 393 + val to_params : t -> Json.t 394 + end 395 + 396 + module Result : sig 397 + type completion = { 398 + values: string list; 399 + has_more: bool option; 400 + total: int option; 401 + } 402 + 403 + type t = { 404 + completion: completion; 405 + meta: Json.t option; 406 + } 407 + 408 + val yojson_of_completion : completion -> Json.t 409 + val completion_of_yojson : Json.t -> completion 410 + 411 + val yojson_of_t : t -> Json.t 412 + val t_of_yojson : Json.t -> t 413 + 414 + val create : completion:completion -> ?meta:Json.t -> unit -> t 415 + val to_result : t -> Json.t 416 + end 417 + end 418 + 317 419 (** Parse a JSON message into an MCP message *) 318 420 val parse_message : Json.t -> JSONRPCMessage.t 319 421 ··· 321 423 val create_notification : ?params:Json.t option -> method_:string -> unit -> JSONRPCMessage.t 322 424 val create_request : ?params:Json.t option -> ?progress_token:ProgressToken.t option -> id:RequestId.t -> method_:string -> unit -> JSONRPCMessage.t 323 425 val create_response : id:RequestId.t -> result:Json.t -> JSONRPCMessage.t 324 - val create_error : id:RequestId.t -> code:int -> message:string -> ?data:Json.t option -> unit -> JSONRPCMessage.t 426 + val create_error : id:RequestId.t -> code:int -> message:string -> ?data:Json.t option -> unit -> JSONRPCMessage.t 427 + 428 + (** Helper functions for common requests/responses *) 429 + val create_completion_request : id:RequestId.t -> argument:Completion.Argument.t -> ref:Completion.Request.reference -> JSONRPCMessage.t 430 + val create_completion_response : id:RequestId.t -> values:string list -> ?has_more:bool option -> ?total:int option -> ?meta:Json.t option -> unit -> JSONRPCMessage.t