Repository navigation
Is there any sane way of using the MCP Prompts between the Client and Server? #4996
Replies: 1 comment
|
Your current direction is basically correct; the missing piece is that MCP prompts and Spring AI prompts are different domain types, so Spring AI 1.1.0 does not automatically pass a An MCP prompt is a server-side prompt function. The client supplies the arguments in On the client, adapt those MCP messages once and then pass a normal Spring AI GetPromptResult result = mcpClient.getPrompt(
new GetPromptRequest("greeting", Map.of("name", "Alice"))
);
List<Message> messages = result.messages().stream()
.map(message -> {
TextContent text = (TextContent) message.content();
return switch (message.role()) {
case USER -> new UserMessage(text.text());
case ASSISTANT -> new AssistantMessage(text.text());
};
})
.toList();
String response = chatClient
.prompt(new Prompt(messages))
.call()
.content();For a production adapter, also handle non-text content explicitly instead of casting blindly. On the server, using String message = "Hello, " + name + "! How can I help you today?";
return new GetPromptResult(
"Greeting",
List.of(new PromptMessage(Role.USER, new TextContent(message)))
);I would use So the idiomatic flow is:
References:
|
Uh oh!
There was an error while loading. Please reload this page.
Hi,
I’m struggling to piece together how to seamlessly use the MCP Prompt from an MCP server to an MCP client, both using Spring AI 1.1.0.
The Problem
The server returns a GetPromptResult which contains a PromptMessage, but neither of these directly correlates to what a ChatClient expects.
The implementation also doesn’t make things easy.
The example shows:
However, to build the message string, it seems I can’t use the PromptTemplate or AssistantPromptTemplate directly.
Instead, I end up having to do something like:
Honestly, this feels unnecessarily complicated.
It would make more sense if the server just returned the prompt with placeholders to the client, and then let the client resolve and fill them.
But even that doesn’t seem straightforward.
Example client usage:
I’d appreciate it if someone could point out if my current approach is completely wrong and suggest a better, more idiomatic way to handle this scenario.
All reactions