Springing into AI - Part 16: MCP Server - Prompts Playground
Project
For a sample playground, we being the owners of Hobbits Inc will be creating pre-defined prompts to aid end users that they can use directly to query with the LLM. Since we do not have our own MCP Client yet, we will be testing the prompt capabilities that we provide using MCP Inspector. An architecture overview of our tools playground is shown below:
Setup
Our project setup encompasses the following:- Java: 17
- Spring AI: 1.1.2
- Spring Boot: 4.0.3
- Testing tool: MCP Inspector
- Source code: MCP Server ( Prompts ) can be viewed here
- Project Demo: Youtube ( MCP Server - Prompts ) here
- Java: 17
- Spring AI: 1.1.2
- Spring Boot: 4.0.3
- Testing tool: MCP Inspector
- Source code: MCP Server ( Prompts ) can be viewed here
- Project Demo: Youtube ( MCP Server - Prompts ) here
Demo Screenshots
We have a generic greeting prompt, which is usually good prompt in real world to provide to the end user should they wish to use with LLM.
For our playground, we have presented different responses for the prompt that can be enacted upon to be shown to user depending on the role. For instance the assistant role is more of a response from the LLM to the end user, while the user role response is what would actually be send as a predefined prompt to the LLM. The type shown above in response is also the different type of response such as text, resource that can be used in variations of the response that would be shown.
We can also use auto-complete pre-defined prompt templates where say if a client is designed in a way that user selects the pre-defined prompt template, then they can be requested to provide suggestions from a pre-compiled list that they can then select.
The section "Enter hobbit" is a dummy auto-complete which accepts a value from a pre-compiled list as mentioned above. The dummy response for one of the selected option is shown:
As can be seen in payload above, the response in this case is of assistant role that we send from the MCP server. What this means is that the client can be shown this, and then should the end user wish to obtain factual information about the hobbit, they can request it as it is a resource type content shown by uri element that can be invoked by LLM to the MCP server, provided that MCP server providing resource exist.
We have a generic greeting prompt, which is usually good prompt in real world to provide to the end user should they wish to use with LLM.
We can also use auto-complete pre-defined prompt templates where say if a client is designed in a way that user selects the pre-defined prompt template, then they can be requested to provide suggestions from a pre-compiled list that they can then select.
As can be seen in payload above, the response in this case is of assistant role that we send from the MCP server. What this means is that the client can be shown this, and then should the end user wish to obtain factual information about the hobbit, they can request it as it is a resource type content shown by uri element that can be invoked by LLM to the MCP server, provided that MCP server providing resource exist.
Code Walkthrough
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
@McpComplete(
prompt = "hobbit-info"
)
public List<String> searchableHobbits(
final McpSchema.CompleteRequest.CompleteArgument argument) {
if ("hobbit".equals(argument.name())) {
return MIDDLE_EARTH_USERS.stream()
.filter(name ->
name.toLowerCase().startsWith(argument.value().toLowerCase()))
.toList();
}
return List.of();
}
@McpPrompt(
name = "hobbit-info",
description = "Provides aid in getting adequate information about a hobbit"
)
public McpSchema.PromptMessage hobbitInfoPrompt(
@McpArg(required = true, name = "hobbit",
description = "Name of the hobbit to search information for") String hobbit) {
return new McpSchema.PromptMessage(McpSchema.Role.ASSISTANT,
new McpSchema.ResourceLink("Hobbit Profile",
"Hobbit Profile",
String.format("hobbit-profile://profile/%s".formatted(hobbit)),
"Provides profile details about the hobbit %s".formatted(hobbit),
null,
null,
null,
null));
}
From the code above:- Line 1 - 4: McpComplete annotation that is used to provide auto-complete suggestion. The prompt metadata ties the complete to the name of the McpPrompt that we see one line 16.
- Line 5: CompleteArgument class is used to obtain the name of the argument from the element for which we wish to query and provide auto-suggestions against.
- Line 6 - 12: Here we merely check if the argument is named hobbit. This name must match the name of the argument that we specify as metadata for the prompt argument that we see on line 20. It is this mechanism that binds the auto-complete information to the argument for the prompt. The logic inside this funciton is merely just validating the user selected input against the list of allowed values and returning result accordingly.
- Line 15 - 19: McpPrompt annotation is used to declare the pre-defined prompt that we would be providing from the MCP Server. The annotation metadata is used to provide the name and description of it that is shown to the user.
- Line 20 - 21: As mentioned earlier, the McpArg specifies an argument that is required. It is to be noted that this doesnt always have to be auto-complete. For this playground we have chosen to demonstrate it along with auto-complete. Should a standalone input be required, the McpComplete can be left out.
- Line 23 - 31: We return an instance of PromptMessage specifying the role (can be USER, ASSISTANT), followed by a type of Content. Out of the box, the model context protocol spec defines the following implementations shown below:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | @McpComplete( prompt = "hobbit-info" ) public List<String> searchableHobbits( final McpSchema.CompleteRequest.CompleteArgument argument) { if ("hobbit".equals(argument.name())) { return MIDDLE_EARTH_USERS.stream() .filter(name -> name.toLowerCase().startsWith(argument.value().toLowerCase())) .toList(); } return List.of(); } @McpPrompt( name = "hobbit-info", description = "Provides aid in getting adequate information about a hobbit" ) public McpSchema.PromptMessage hobbitInfoPrompt( @McpArg(required = true, name = "hobbit", description = "Name of the hobbit to search information for") String hobbit) { return new McpSchema.PromptMessage(McpSchema.Role.ASSISTANT, new McpSchema.ResourceLink("Hobbit Profile", "Hobbit Profile", String.format("hobbit-profile://profile/%s".formatted(hobbit)), "Provides profile details about the hobbit %s".formatted(hobbit), null, null, null, null)); } |
- Line 1 - 4: McpComplete annotation that is used to provide auto-complete suggestion. The prompt metadata ties the complete to the name of the McpPrompt that we see one line 16.
- Line 5: CompleteArgument class is used to obtain the name of the argument from the element for which we wish to query and provide auto-suggestions against.
- Line 6 - 12: Here we merely check if the argument is named hobbit. This name must match the name of the argument that we specify as metadata for the prompt argument that we see on line 20. It is this mechanism that binds the auto-complete information to the argument for the prompt. The logic inside this funciton is merely just validating the user selected input against the list of allowed values and returning result accordingly.
- Line 15 - 19: McpPrompt annotation is used to declare the pre-defined prompt that we would be providing from the MCP Server. The annotation metadata is used to provide the name and description of it that is shown to the user.
- Line 20 - 21: As mentioned earlier, the McpArg specifies an argument that is required. It is to be noted that this doesnt always have to be auto-complete. For this playground we have chosen to demonstrate it along with auto-complete. Should a standalone input be required, the McpComplete can be left out.
- Line 23 - 31: We return an instance of PromptMessage specifying the role (can be USER, ASSISTANT), followed by a type of Content. Out of the box, the model context protocol spec defines the following implementations shown below:

Comments
Post a Comment