Quick revision
The short answer
We add Spring AI's MCP server starter, the WebMVC or WebFlux variant, write a searchTickets method annotated with @Tool with typed parameters such as query, status and a limit capped at a maximum, and publish it through a ToolCallbackProvider bean. The HTTP endpoint is secured as an OAuth 2 resource server that validates bearer tokens, as the MCP authorization flow expects, and each tool checks the caller's permissions. We validate inputs, cap result sizes, log every call with the caller's identity, and version tool names and schemas carefully because clients cache them.
What to remember
In this answer, we built an MCP server in Spring Boot. We added the Spring AI MCP server starter and OAuth 2 resource server support, wrote searchTickets as a @Tool with typed parameters and a capped limit and published it through a ToolCallbackProvider bean, secured the endpoint with bearer tokens and per-tool permission checks, validated inputs and capped results, logged every call with the caller's identity, and versioned tool names and schemas because clients cache them.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
We add Spring AI's MCP server starter, the WebMVC or WebFlux variant, write a searchTickets method annotated with @Tool with typed parameters such as query, status and a limit capped at a maximum, and publish it through a ToolCallbackProvider bean. The HTTP endpoint is secured as an OAuth 2 resource server that validates bearer tokens, as the MCP authorization flow expects, and each tool checks the caller's permissions. We validate inputs, cap result sizes, log every call with the caller's identity, and version tool names and schemas carefully because clients cache them.
Why this question is asked
Many companies want their internal AI assistants to search systems such as the ticket tracker. Building an MCP server for it means every MCP-capable assistant can use the same search without custom integration. Interviewers ask this coding question because it combines Spring Boot setup, tool design and security, and the security part is easy to get wrong when the caller is a model. In this answer, we will add the dependencies, write the tool, register it, secure the endpoint, protect the data with limits, and plan logging and schema changes.
1A Quick Recap of an MCP Server
An MCP server offers tools, functions with a name, a description and a JSON schema for their parameters, that AI applications discover with a tools/list request and invoke with tools/call. For internal assistants on other machines, the server is reached over HTTP using the Streamable HTTP transport. Spring AI builds on the official MCP Java SDK and can turn ordinary @Tool methods into MCP tools, so most of the protocol work is done for us.
2Adding the Dependencies
We add the MCP server starter for our web stack, plus Spring Security's OAuth 2 resource server support. With Spring MVC the starter is the WebMVC variant; with WebFlux, the WebFlux variant. The starter names below are those of Spring AI 1.x; check your version, because they were renamed before 1.0 and transport options are still evolving.
The server name and version are reported to clients during initialisation. The issuer URI tells Spring Security which identity provider's tokens to trust.
3Writing the searchTickets Tool
The tool has typed parameters with clear descriptions and returns a small structured result. Notice the limit: the model may ask for any number, so the method clamps it to a maximum.
The ToolCallbackProvider bean is how the MCP server starter finds the tools to publish; MethodToolCallbackProvider builds tool definitions from the @Tool methods, including a JSON schema in which status becomes an enum of three values. Newer Spring AI versions also offer MCP-specific annotations; check your version.
The search itself receives the caller, so the service only returns tickets from teams the caller may see. Fetching n + 1 rows lets us tell the model whether more results exist, which helps it ask a narrower question rather than repeating the search.
4Securing the HTTP Endpoint
The MCP specification defines authorization for HTTP transports on top of OAuth: the MCP server acts as a protected resource, and clients send an access token as a bearer token with every request. In Spring this means configuring the application as an OAuth 2 resource server that validates JWTs from the company identity provider, and requiring a scope such as tickets.read for the MCP endpoint.
7 messages in order. Play them, or step through them one at a time.
The scope check protects the endpoint, but the important check happens per tool and per row: the tool passes the caller to the service, which applies the same team permissions as the ticket UI. Ideally the token belongs to the end user on whose behalf the assistant acts, not to a shared assistant account, so each person only finds what they could find themselves. The endpoint path depends on the starter's configuration, so check it for your version.
5Logging and Changing Tools Safely
Every tool call is logged with the caller's identity, the client application, the arguments, the number of results and the duration. That lets us answer who searched for what, detect a client that suddenly searches thousands of times, and debug wrong answers.
Tool names and schemas are a contract. Clients fetch tools/list and often cache it, and models may have been evaluated against the old description. So we avoid renaming tools or changing parameter meanings in place. We add optional parameters in a backwards-compatible way, introduce a new tool such as searchTicketsV2 for breaking changes, announce the deprecation, keep both for a period, and send a tools list-changed notification where clients support it.
6Follow-up questions
Should validation errors be exceptions or results?
For clearly invalid input such as an empty query, an exception that the framework turns into a tool error is fine, because the model gets a clear message and can correct itself. For normal outcomes such as no matching tickets, return an empty result with returned = 0, not an error.
Why limit results if the user is allowed to see all those tickets?
Because the model's context and our cost are limited, and large results confuse the model. A cap also limits the damage if a token is stolen or a prompt injection tries to extract data in bulk.
How would you test the server?
Unit test TicketTools with a mocked security context and service, including the limit clamp. Then run an integration test with the MCP Java SDK client against the running application: call tools/list, check the schema, and call searchTickets with and without a valid token.
7Conclusion
In this answer, we built an MCP server in Spring Boot. We added the Spring AI MCP server starter and OAuth 2 resource server support, wrote searchTickets as a @Tool with typed parameters and a capped limit and published it through a ToolCallbackProvider bean, secured the endpoint with bearer tokens and per-tool permission checks, validated inputs and capped results, logged every call with the caller's identity, and versioned tool names and schemas because clients cache them.
Open with
I would start with Spring AI's MCP server starter, the WebMVC variant for a servlet app, plus Spring Security's OAuth 2 resource server, and check the exact starter names for our Spring AI version.
Then, if they want more
The tool is a @Tool method searchTickets on a service bean, with @ToolParam descriptions and typed parameters: a query string, an optional status enum, and an optional limit that I clamp to a maximum like 25 no matter what the model asks. It returns a small record list with id, title, status, team and updated date, plus a truncated flag.
I register it through a ToolCallbackProvider bean, MethodToolCallbackProvider with the tool object, which is how the starter publishes it over Streamable HTTP. For security, the MCP spec builds authorization on OAuth, so the endpoint validates bearer JWTs from our identity provider and requires a scope like tickets.read.
More important, the tool passes the caller's identity to the ticket service, which applies the same team permissions as the normal UI, ideally with the end user's own token. I validate the query length, cap results, and log each call with the caller, client, arguments and result count.
And I treat tool names and schemas as a contract: additive changes only, a new tool for breaking changes, and deprecations announced, because clients cache tools/list.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- Spring AI reference
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "Build an MCP server in a Spring Boot application that exposes a searchTickets tool over...": https://scrivio-interview-library.netlify.app/spring-boot/ai-agents-and-mcp/9a806df67b/