You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expose the Farm Data Model REST API as a Model Context Protocol (MCP) server, enabling AI assistants (Claude Desktop, VS Code Copilot, LangChain clients) to programmatically interact with farm data through the standardized MCP protocol.
The MCP server will be embedded in the existing fdm-api package using @hono/mcp for native Hono integration, sharing the same service layer (FdmApiServices) used by the REST routes.
Motivation
AI-native access: LLMs can directly read farm data and perform operations without custom HTTP client code
Standardized protocol: MCP is becoming the universal standard for LLM ↔ tool communication
Broad client support: Works with Claude Desktop, VS Code Copilot, Cursor, LangChain, and any MCP-compatible client
Complements fdm-agents: Provides atomic MCP tools that fdm-agents (LangGraph orchestrator) can consume as a tool provider
Standalone entry point (mcp-server.ts) using StdioServerTransport
Authentication
API key passthrough — same mechanism as the REST API:
HTTP transport: Existing createApiKeyAuth middleware validates X-API-Key / Authorization: Bearer before the MCP handler
stdio transport: API key configured via FDM_API_KEY environment variable
Audit channel: Must use channel: "mcp" (not "api") in the principal context and withAuditContext call so that MCP-originated mutations are distinguishable from REST API calls in audit logs
Peer dependencies already satisfied by fdm-api: hono ^4.12.18, zod ^4.4.3.
Integration Pattern
import{McpServer}from'@modelcontextprotocol/sdk/server/mcp.js'import{StreamableHTTPTransport}from'@hono/mcp'// Create MCP server — name derived from PUBLIC_FDM_NAME env var (same as REST API)constmcpServer=newMcpServer({name: `${appName}-mcp-server`,// e.g. "FDM-mcp-server" or custom nameversion: '0.1.0',},{instructions: `You are connected to ${appName}. You can read farm data (farms, fields, cultivations, soil analyses, etc.) and perform write operations (create, update, delete). Use resources to browse available data before making changes. All dates use YYYY-MM-DD format.`,})// Mount on Hono appapp.all('/mcp',async(c)=>{consttransport=newStreamableHTTPTransport({enableDnsRebindingProtection: true,sessionIdGenerator: ()=>crypto.randomUUID(),})awaitmcpServer.connect(transport)returntransport.handleRequest(c)})
Security
DNS rebinding protection via @hono/mcp (enableDnsRebindingProtection, allowedOrigins from FDM_API_ALLOWED_ORIGINS)
Session management via mcp-session-id headers
Same rate limiting as REST API applied to MCP endpoint
API key validation before MCP handler processes requests
MCP server name derived from PUBLIC_FDM_NAME env var (consistent with REST API naming)
MCP Resources (read-only, browsable context)
Resources provide LLMs with structured read access to farm data:
URI Template
Description
fdm://farms
List all farms for the authenticated user
fdm://farms/{b_id_farm}
Single farm detail
fdm://farms/{b_id_farm}/fields
Fields belonging to a farm
fdm://fields/{b_id_field}
Single field detail (with GeoJSON geometry)
fdm://fields/{b_id_field}/cultivations
Cultivations on a field
fdm://fields/{b_id_field}/harvests
Harvests for a field
fdm://fields/{b_id_field}/soil-analyses
Soil analyses for a field
fdm://fields/{b_id_field}/fertilizer-applications
Fertilizer applications on a field
fdm://fields/{b_id_field}/measures
Measures on a field
fdm://farms/{b_id_farm}/fertilizers
Fertilizers available to a farm
fdm://farms/{b_id_farm}/organic-certifications
Organic certifications for a farm
fdm://farms/{b_id_farm}/derogations
Derogations for a farm
fdm://farms/{b_id_farm}/grazing-intentions
Grazing intentions for a farm
MCP Tools (actions)
Tools enable LLMs to perform write operations and run calculations:
Summary
Expose the Farm Data Model REST API as a Model Context Protocol (MCP) server, enabling AI assistants (Claude Desktop, VS Code Copilot, LangChain clients) to programmatically interact with farm data through the standardized MCP protocol.
The MCP server will be embedded in the existing
fdm-apipackage using@hono/mcpfor native Hono integration, sharing the same service layer (FdmApiServices) used by the REST routes.Motivation
fdm-agents(LangGraph orchestrator) can consume as a tool providerArchitecture
Technical Design
Transport
@hono/mcpStreamableHTTPTransportmounted on/mcpmcp-server.ts) usingStdioServerTransportAuthentication
API key passthrough — same mechanism as the REST API:
createApiKeyAuthmiddleware validatesX-API-Key/Authorization: Bearerbefore the MCP handlerFDM_API_KEYenvironment variablechannel: "mcp"(not"api") in the principal context andwithAuditContextcall so that MCP-originated mutations are distinguishable from REST API calls in audit logsDependencies
{ "@modelcontextprotocol/sdk": "^1.25.1", "@hono/mcp": "^0.3.0" }Peer dependencies already satisfied by fdm-api:
hono ^4.12.18,zod ^4.4.3.Integration Pattern
Security
@hono/mcp(enableDnsRebindingProtection,allowedOriginsfromFDM_API_ALLOWED_ORIGINS)mcp-session-idheadersPUBLIC_FDM_NAMEenv var (consistent with REST API naming)MCP Resources (read-only, browsable context)
Resources provide LLMs with structured read access to farm data:
fdm://farmsfdm://farms/{b_id_farm}fdm://farms/{b_id_farm}/fieldsfdm://fields/{b_id_field}fdm://fields/{b_id_field}/cultivationsfdm://fields/{b_id_field}/harvestsfdm://fields/{b_id_field}/soil-analysesfdm://fields/{b_id_field}/fertilizer-applicationsfdm://fields/{b_id_field}/measuresfdm://farms/{b_id_farm}/fertilizersfdm://farms/{b_id_farm}/organic-certificationsfdm://farms/{b_id_farm}/derogationsfdm://farms/{b_id_farm}/grazing-intentionsMCP Tools (actions)
Tools enable LLMs to perform write operations and run calculations:
Farm Management
create_farmupdate_farmdelete_farmField Management
create_fieldupdate_fielddelete_fieldCultivation Management
create_cultivationupdate_cultivationdelete_cultivationHarvest Management
create_harvestupdate_harvestdelete_harvestFertilizer Management
create_fertilizerdelete_fertilizersearch_fertilizer_catalogueFertilizer Application Management
create_fertilizer_applicationupdate_fertilizer_applicationdelete_fertilizer_applicationSoil Analysis Management
create_soil_analysisupdate_soil_analysisdelete_soil_analysisMeasure Management
create_measureupdate_measuredelete_measureOrganic Certification Management
create_organic_certificationdelete_organic_certificationDerogation Management
create_derogationdelete_derogationGrazing Intention Management
set_grazing_intentiondelete_grazing_intentionAgronomic Calculations
calculate_nitrogen_balanceget_nitrogen_balance_fieldcalculate_organic_matter_balanceget_organic_matter_balance_fieldget_dose_for_fieldget_fertilization_normsFile Structure
Client Configuration Examples
Claude Desktop (
claude_desktop_config.json){ "mcpServers": { "your-app-name": { "command": "node", "args": ["path/to/fdm-api/dist/mcp-server.js"], "env": { "FDM_API_KEY": "your_api_key_here", "PUBLIC_FDM_NAME": "Your App Name", "POSTGRES_HOST": "localhost", "POSTGRES_PORT": "5432", "POSTGRES_DB": "fdm", "POSTGRES_USER": "...", "POSTGRES_PASSWORD": "...", "BETTER_AUTH_SECRET": "..." } } } }VS Code Copilot (
.vscode/mcp.json){ "servers": { "your-app-name": { "type": "http", "url": "https://api.example.com/mcp", "headers": { "X-API-Key": "your_api_key_here" } } } }LangChain / Custom Client
Implementation Tasks
@modelcontextprotocol/sdkand@hono/mcpdependenciesmcp/index.ts— McpServer factory with instructions and capability registrationmcp/auth.ts— API key verification adapter for MCP contextmcp/transport.ts— MountStreamableHTTPTransporton/mcpin Hono appmcp-server.tsstdio entry pointDesign Considerations
@modelcontextprotocol/client, unifying the tool layerAcceptance Criteria
/mcpendpoint