Observability
MCP server
Let AI assistants use your application's data and actions safely.
What is MCP?
MCP (the Model Context Protocol) is an open standard that lets an AI assistant (Claude, an IDE agent, a custom agent) use tools that you provide. Instead of the assistant guessing, it can ask your application: "find product A1", "how many orders are late?", "restock this item". Your application stays in control: it decides what is offered, who may call it and what each call can do.
An MCP server offers three things:
| Primitive | Meaning | Example |
|---|---|---|
| Tool | an action the AI can call | find_product(sku), restock(sku, amount) |
| Resource | data the AI can read at a fixed address | inventory://low-stock |
| Prompt | a reusable, ready-made question | restock_plan(category) |
Build one in three steps
1. Mark methods of your normal services. The method's parameters become the tool's inputs, its description tells the AI when to use it:
@Injectable
class Inventory(private val repo: ProductRepository) {
@McpTool("find_product", "Looks a product up by its SKU. Returns null when there is none.", readOnly = true)
fun find(sku: String): Product? = repo.findBySku(sku)
@McpTool("restock", "Adds stock to a product. Changes data.", destructive = false)
fun restock(sku: String, amount: Int, note: String?): String { repo.add(sku, amount); return "restocked $sku by $amount" }
@McpResource("inventory://low-stock", "Products running low", "application/json")
fun lowStock(): List<Product> = repo.lowStock()
@McpPrompt("restock_plan", "Drafts a restock plan")
fun plan(category: String) = "Plan a restock for $category using the inventory://low-stock resource."
}2. Import the module and set the rules:
Kest.run<AppModule> {
import(McpModule.forRoot {
name = "inventory"; version = "1.0.0"
instructions = "Use find_product before restock. Never restock more than 1000 at once." // shown to the AI
bearerToken = System.getenv("MCP_TOKEN") // required: Authorization: Bearer ...
allowedOrigins = setOf("https://app.example.com") // protects against browser attacks
authorize = { request -> request.header("x-team") == "ops" } // any extra check
})
}3. Mount it in the HTTP server: app.http(adapter) { useMiddleware(mcp.middleware("/mcp")) }. Point the assistant at https://your-host/mcp with the token.
Rules for good tools
- Describe them for a reader who cannot see your code. The description is the AI's only guide: say what it does, what it returns and when not to use it.
- Mark
readOnly = truefor tools that only read, anddestructive = truefor ones that delete or cannot be undone; assistants use this to decide when to ask the human first. - Optional parameters must be nullable (
note: String?). Kotlin default values are not visible to the framework; a non-nullable parameter is required. - Validate inside the tool as you would for an HTTP endpoint: an AI can send wrong values. A tool that throws is reported back to the model as an error (
isError), so it can correct itself, not as a crash. - Least privilege. Offer only what the assistant needs, scope the token, and put dangerous actions behind your own limits (the
1000above), not behind the AI's good manners. - Return small, useful data, not whole database rows.
- Also give the same method to your own agents with
@AiTool(AI tools).
Testing
val mcp = McpTestClient(module.get<McpServer>())
assertEquals("restocked A1 by 5", mcp.call("restock", mapOf("sku" to "A1", "amount" to 5)).text)See MCP testing. Details of the protocol support are in Transports (HTTP only; no stdio).