An MCP Server in C#
Summary
A JSON-RPC handler exposes five read-only research tools and refuses any tool name outside that list.
Companion code: 05-mcp-server in indian-legal-ai-lab.
Previous: Part 4: Function Calling and Legal Research Tools
Another process should call the same reads without sharing the C# object. `McpStdioServer` answers one JSON-RPC request by calling `LegalResearchLibrary`.
The methods it accepts
`Handle` reads `method` and switches:
- `initialize` returns protocol version `2024-11-05`, empty tool, resource, and prompt capabilities, and the server name `indian-legal-ai-lab`.
- `tools/list` returns five tools: `search_cases`, `get_case_metadata`, `get_judgment`, `find_cited_cases`, `lookup_statute`.
- `resources/list` returns `legal://statutes/article-21`.
- `prompts/list` returns a prompt named `case-brief`.
- `tools/call` dispatches on `params.name`.
An unknown JSON-RPC method returns error code `-32601`. A tool name that is not in the switch, including `delete_corpus`, still returns a result whose text says the tool is not on the allow list. The injection fixture in the corpus asks a reader to call `delete_corpus`. The server refuses that name because the name is not a case branch.
object payload = name switch
{
"search_cases" => _library.SearchCases(Arg("query"), ...),
"get_case_metadata" => _library.GetCaseMetadata(Arg("caseId")),
"get_judgment" => _library.GetJudgment(Arg("caseId")),
"find_cited_cases" => _library.FindCitedCases(Arg("caseId")),
"lookup_statute" => _library.LookupStatute(Arg("query")),
_ => ToolResult<string>.Fail("Tool is not on the allow list.")
};This is an in-process handler used by the lab and the tests. It is not the official MCP C# SDK, and it does not frame stdin with `Content-Length`.
dotnet run --project labs/05-mcp-serverThe process prints the initialize result and the tool list. `Mcp_lists_read_only_tools` asserts the five names.
Official SDK host
`OfficialMcpHost` registers `ModelContextProtocol` 1.4.0, which targets `net8.0`, with stdio transport and `LegalMcpTools`. Run it with `dotnet run --project src/LegalAiLab.McpHost`. Logging is configured toward standard error. A client round-trip over stdio was not executed in the recorded test run. The lab command still prints the teaching JSON handler, and the article above describes that handler.
Next
Part 6: A Bounded Legal Research Agent
Verified in this revision
The primary host is `LegalAiLab.McpHost`, which calls `OfficialMcpHost` on MCP SDK 1.4.0 stdio. Lab 05 prints that command and then the teaching JSON handler. `Mcp_stdio_client_lists_and_calls_search` starts that host with `StdioClientTransport`, lists tools, and calls `lookup_statute` for Article 21. The teaching handler is not that round-trip.
More in Applied AI
Function Calling and Legal Research Tools
Deterministic C# tools for case metadata, passages, and a short constitutional catalog, with rejected blank input.
A Bounded Legal Research Agent
A tool loop with a call limit and a timeout that refuses outcome predictions and cites only retrieved passages.
LLM Fundamentals in C#
A supplied judgment can be summarized only from passages you pass in. The same client, with no passage, names a citation that the catalog rejects.