Skip to content

Commit 47af4a3

Browse files
authored
Merge pull request #79 from zircote/docs/add-mcp-module-reference-1a5bcd173af6a0e9
2 parents 467c0f0 + f3ec773 commit 47af4a3

File tree

1 file changed

+215
-0
lines changed

1 file changed

+215
-0
lines changed

docs/reference/LIBRARY-API.md

Lines changed: 215 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,223 @@ Min/max bounds for filtering animals by trait value in search criteria.
561561

562562
---
563563

564+
## mcp Module
565+
566+
The `mcp` module provides a complete Model Context Protocol server implementation with tools, resources, prompts, and analytics for livestock breeding intelligence.
567+
568+
### NsipServer
569+
570+
MCP server implementation that exposes 13 tools, 5 static resources, 4 resource templates, and 7 guided breeding prompts.
571+
572+
```rust
573+
use nsip::mcp::NsipServer;
574+
575+
let server = NsipServer::new();
576+
```
577+
578+
#### `NsipServer::new() -> Self`
579+
580+
Create a new MCP server instance with default NSIP API client.
581+
582+
```rust
583+
use nsip::mcp::NsipServer;
584+
585+
let server = NsipServer::new();
586+
```
587+
588+
#### `serve_stdio() -> Result<()>`
589+
590+
Start the MCP server on stdio transport (used by Claude Desktop, Claude Code, Cursor, etc.).
591+
592+
```rust
593+
use nsip::mcp::serve_stdio;
594+
595+
#[tokio::main]
596+
async fn main() -> nsip::Result<()> {
597+
serve_stdio().await
598+
}
599+
```
600+
601+
---
602+
603+
### analytics Submodule
604+
605+
Pure computation functions for breeding analytics with no external dependencies.
606+
607+
#### Types
608+
609+
**`CoiRating`** — Traffic-light rating for inbreeding coefficient:
610+
- `Green` — COI < 6.25% (acceptable)
611+
- `Yellow` — 6.25% ≤ COI < 12.5% (elevated, proceed with caution)
612+
- `Red` — COI ≥ 12.5% (high inbreeding, generally avoid)
613+
614+
**`SharedAncestor`** — Common ancestor found in both sire and dam pedigrees.
615+
616+
| Field | Type | Description |
617+
|-------|------|-------------|
618+
| `lpn_id` | `String` | LPN ID of the common ancestor |
619+
| `sire_depth` | `usize` | Generations from sire to this ancestor |
620+
| `dam_depth` | `usize` | Generations from dam to this ancestor |
621+
622+
**`CoiResult`** — Result of coefficient of inbreeding calculation.
623+
624+
| Field | Type | Description |
625+
|-------|------|-------------|
626+
| `coefficient` | `f64` | Wright's coefficient of inbreeding (0.0–1.0) |
627+
| `rating` | `CoiRating` | Traffic-light rating |
628+
| `shared_ancestors` | `Vec<SharedAncestor>` | Common ancestors contributing to inbreeding |
629+
630+
**`RankedAnimal`** — Animal with weighted composite score for trait-based ranking.
631+
632+
| Field | Type | Description |
633+
|-------|------|-------------|
634+
| `lpn_id` | `String` | LPN identifier |
635+
| `score` | `f64` | Weighted composite score |
636+
| `trait_scores` | `HashMap<String, f64>` | Per-trait weighted scores |
637+
638+
#### Functions
639+
640+
**`calculate_coi(sire_lineage: &Lineage, dam_lineage: &Lineage) -> CoiResult`**
641+
642+
Calculate Wright's coefficient of inbreeding from sire and dam pedigrees.
643+
644+
Formula: `COI = Σ [(0.5)^(n₁ + n₂ + 1)]` where:
645+
- `n₁` = path length from sire to common ancestor
646+
- `n₂` = path length from dam to common ancestor
647+
648+
```rust
649+
use nsip::{NsipClient, mcp::analytics::calculate_coi};
650+
651+
# async fn example() -> nsip::Result<()> {
652+
let client = NsipClient::new();
653+
let sire_lineage = client.lineage("430735-0032").await?;
654+
let dam_lineage = client.lineage("430735-0089").await?;
655+
656+
let coi_result = calculate_coi(&sire_lineage, &dam_lineage);
657+
println!("COI: {:.2}% ({:?})", coi_result.coefficient * 100.0, coi_result.rating);
658+
# Ok(())
659+
# }
660+
```
661+
662+
**`rank_animals(animals: &[AnimalDetails], weights: &HashMap<String, f64>) -> Vec<RankedAnimal>`**
663+
664+
Rank animals by weighted composite of EBV traits.
665+
666+
Score formula: `Σ (trait_value × weight × accuracy/100)` for each trait.
667+
668+
```rust
669+
use std::collections::HashMap;
670+
use nsip::{NsipClient, mcp::analytics::rank_animals};
671+
672+
# async fn example() -> nsip::Result<()> {
673+
let client = NsipClient::new();
674+
let search = client.search(
675+
nsip::SearchCriteria::new()
676+
.with_breed_id(486)
677+
.with_gender("Male")
678+
.with_status("CURRENT")
679+
).await?;
680+
681+
let weights = HashMap::from([
682+
("BWT".to_string(), -1.0),
683+
("WWT".to_string(), 2.0),
684+
("YWT".to_string(), 1.5),
685+
]);
686+
687+
let ranked = rank_animals(&search.animals, &weights);
688+
for animal in ranked.iter().take(5) {
689+
println!("{}: {:.2}", animal.lpn_id, animal.score);
690+
}
691+
# Ok(())
692+
# }
693+
```
694+
695+
**`trait_complementarity(sire: &AnimalDetails, dam: &AnimalDetails) -> HashMap<String, f64>`**
696+
697+
Predict midparent EBV values for potential offspring.
698+
699+
Formula: `predicted_EBV = (sire_EBV + dam_EBV) / 2.0`
700+
701+
```rust
702+
use nsip::{NsipClient, mcp::analytics::trait_complementarity};
703+
704+
# async fn example() -> nsip::Result<()> {
705+
let client = NsipClient::new();
706+
let sire = client.details("430735-0032").await?;
707+
let dam = client.details("430735-0089").await?;
708+
709+
let midparent_ebvs = trait_complementarity(&sire, &dam);
710+
for (trait_name, value) in midparent_ebvs {
711+
println!("{}: {:.2}", trait_name, value);
712+
}
713+
# Ok(())
714+
# }
715+
```
716+
717+
**`find_shared_ancestors(sire_lineage: &Lineage, dam_lineage: &Lineage) -> Vec<SharedAncestor>`**
718+
719+
Find ancestors that appear in both sire and dam pedigrees.
720+
721+
```rust
722+
use nsip::{NsipClient, mcp::analytics::find_shared_ancestors};
723+
724+
# async fn example() -> nsip::Result<()> {
725+
let client = NsipClient::new();
726+
let sire_lineage = client.lineage("430735-0032").await?;
727+
let dam_lineage = client.lineage("430735-0089").await?;
728+
729+
let shared = find_shared_ancestors(&sire_lineage, &dam_lineage);
730+
for ancestor in shared {
731+
println!("{} (sire depth: {}, dam depth: {})",
732+
ancestor.lpn_id, ancestor.sire_depth, ancestor.dam_depth);
733+
}
734+
# Ok(())
735+
# }
736+
```
737+
738+
---
739+
740+
### prompts Submodule
741+
742+
Guided breeding workflow prompts that fetch live data and construct structured context for LLM-based breeding advice.
743+
744+
7 prompts available:
745+
- `evaluate-ram` — Evaluate a ram's breeding value (emphasizes growth and carcass traits)
746+
- `evaluate-ewe` — Evaluate a ewe's breeding value (emphasizes maternal traits)
747+
- `compare-breeding-stock` — Compare 2-5 animals side-by-side
748+
- `plan-mating` — Plan a specific mating with COI check and trait complementarity
749+
- `flock-improvement` — Analyze a breed/flock for trait gaps
750+
- `select-replacement` — Find top replacement candidates by gender and target trait
751+
- `interpret-ebvs` — Plain-language EBV explanation for farmers
752+
753+
See [MCP Server Reference](../MCP.md#prompt-reference) for full prompt documentation.
754+
755+
---
756+
757+
### resources Submodule
758+
759+
Static and dynamic resources for MCP clients.
760+
761+
**Static resources:**
762+
- `nsip://glossary` — EBV trait glossary (markdown)
763+
- `nsip://breeds` — Live breed directory (JSON)
764+
- `nsip://guide/selection` — Selection guide (markdown)
765+
- `nsip://guide/inbreeding` — Inbreeding guide (markdown)
766+
- `nsip://status` — Database status (JSON)
767+
768+
**Resource templates:**
769+
- `nsip://animal/{lpn_id}` — Full animal profile
770+
- `nsip://animal/{lpn_id}/pedigree` — Pedigree tree
771+
- `nsip://animal/{lpn_id}/progeny` — Offspring list
772+
- `nsip://breed/{breed_id}/ranges` — Breed trait ranges
773+
774+
See [MCP Server Reference](../MCP.md#resource-reference) for full resource documentation.
775+
776+
---
777+
564778
## See Also
565779

780+
- [MCP Server Reference](../MCP.md) -- complete MCP tools, resources, and prompts documentation
566781
- [Error Handling Reference](ERROR-HANDLING.md) -- error types and recovery strategies
567782
- [Configuration Reference](CONFIGURATION.md) -- client configuration options
568783
- [CLI Reference](CLI.md) -- command-line interface for the same functionality

0 commit comments

Comments
 (0)