Skip to content

8.17 Spring AI patterns — the extraction and the MCP server

How the extraction and the MCP server use Spring AI, and the reason wherever FeeCheck departs from the common pattern. The rules that decide a difference are the extraction rules (8.7); the comparison with Spring AI's examples, the versions and the types imported are in the comparison guide (docs/guides/spring-ai-examples-comparison.md).

Pattern FeeCheck Reason for a difference
Fluent ChatClient, answer taken as text ExtractionLoop creates the client per question from the ChatModel it is handed and sets tools and advisors per call, so the stub can play a different model for each rendition the provider is the model handed in, not a bean the builder fixes
Structured output the answer is read as text and parsed by ExtractionResponseParser .entity(…) throws on an unexpected answer, where the component falls back (8.7)
Schema validation with a re-ask StructuredOutputValidationAdvisor over the schema generated from ExtractionClaim, default attempts, no hand-written loop, no minLength —
Tool loop as a recursive advisor ToolCallingAdvisor registered explicitly, its position in the chain given —
Caps on the tool loop DefaultToolCallingManager with calls per tool and in total, ToolCallLimitBehavior.THROW; a stop is turned into the fallback —
Tool definition DocumentLookupTools hands hand-written ToolCallbacks with a ToolDefinition a malformed argument becomes a refusal the model reads, and nothing the model wrote reaches a log (NFR-4)
Extra tool arguments such as confidence tools take their one argument only; confidence is derived from the citation check confidence is never reported by the model (8.7)
Custom CallAdvisor ModelRetryAdvisor has the same shape; ConversationRecorder is a CallAdvisor that keeps the conversation for the re-ask —
Advisor reading token usage LlmCallTelemetry is a CallAdvisor that also times the call, records a failed one and charges the extraction's loop budget a BaseAdvisor's before/after cannot time a call or see its failure
Evaluating an answer and asking again the citation check grades the answer against the document in code; one re-ask names the unconfirmed fields no model grades a model
Retry of a model call Spring AI's retry is off (max-attempts=0); ModelRetryAdvisor applies one policy to every provider on Spring Framework's core retry; TransientAiException and NonTransientAiException are classified per provider —
Prompt handling ExtractionPrompt reads prompts/*.txt as resources with plain Java no framework in the domain ring, and the JSON example would be read as placeholders — see the extraction guide (docs/guides/structured-llm-response.md)
Messages and prompt types in a scripted model ScriptedChatModel plays the stub's tool script as a ChatModel, reading the played calls off the ToolResponseMessages in the prompt; ScriptedToolCall parses arguments with Spring AI's JsonParser —
Decorating a ChatModel JsonToolResults encodes each tool result as JSON for the Google GenAI model, which parses a tool response as JSON a workaround for Spring AI 2.0.2
MCP server tools FeeCheckTools with @McpTool and @McpToolParam, the webmvc starter and the STREAMABLE protocol; the bean is declared by a configuration under the mcp profile STREAMABLE is kept, not STATELESS as in the weather example; both tools declare read-only, closed-world and idempotent hints; extraction_fields publishes an output schema generated from its return type (generateOutputSchema)
Securing the MCP endpoint a security chain for /mcp/** ordered before the sign-in chain; Cloud Run's invoker check guards it (8.8) —

Spring AI features the extraction and the MCP server do not use: retrieval-augmented generation, vector stores and chat memory.