11package com .sap .ai .sdk .app .services ;
22
33import static com .sap .ai .sdk .orchestration .OrchestrationAiModel .GEMINI_1_5_FLASH ;
4+ import static com .sap .ai .sdk .orchestration .OrchestrationAiModel .GPT_4O ;
45import static com .sap .ai .sdk .orchestration .OrchestrationAiModel .GPT_4O_MINI ;
56
67import com .fasterxml .jackson .annotation .JsonProperty ;
2425import org .springframework .ai .chat .client .advisor .MessageChatMemoryAdvisor ;
2526import org .springframework .ai .chat .memory .InMemoryChatMemoryRepository ;
2627import org .springframework .ai .chat .memory .MessageWindowChatMemory ;
28+ import org .springframework .ai .chat .messages .SystemMessage ;
29+ import org .springframework .ai .chat .messages .UserMessage ;
2730import org .springframework .ai .chat .model .ChatModel ;
2831import org .springframework .ai .chat .model .ChatResponse ;
2932import org .springframework .ai .chat .prompt .Prompt ;
3033import org .springframework .ai .chat .prompt .PromptTemplate ;
3134import org .springframework .ai .support .ToolCallbacks ;
35+ import org .springframework .ai .tool .ToolCallbackProvider ;
36+ import org .springframework .beans .factory .annotation .Autowired ;
37+ import org .springframework .beans .factory .annotation .Value ;
3238import org .springframework .stereotype .Service ;
3339import reactor .core .publisher .Flux ;
3440
@@ -40,6 +46,13 @@ public class SpringAiOrchestrationService {
4046 new OrchestrationModuleConfig ().withLlmConfig (GPT_4O_MINI );
4147 private final OrchestrationChatOptions defaultOptions = new OrchestrationChatOptions (config );
4248
49+ @ Nullable
50+ @ Autowired (required = false )
51+ ToolCallbackProvider toolCallbackProvider ;
52+
53+ @ Value ("${spring.profiles.active:}" )
54+ private String activeProfile ;
55+
4356 /**
4457 * Chat request to OpenAI through the Orchestration service with a simple prompt.
4558 *
@@ -172,6 +185,41 @@ public ChatResponse toolCalling(final boolean internalToolExecutionEnabled) {
172185 return client .call (prompt );
173186 }
174187
188+ /**
189+ * Example using an MCP client to use a file system tool. Enabled via dedicated Spring profile,
190+ * since it requires an actual MCP server to run.
191+ *
192+ * @return the assistant response object
193+ */
194+ @ Nonnull
195+ public ChatResponse toolCallingMcp () {
196+ // check if spring profile is set to 'mcp'
197+ if (!activeProfile .equals ("mcp" )) {
198+ throw new IllegalStateException (
199+ "The 'mcp' Spring profile is not active. Set it, e.g. by passing a JVM parameter '-Dspring.profiles.active=mcp'." );
200+ }
201+ if (toolCallbackProvider == null ) {
202+ throw new IllegalStateException (
203+ "No MCP clients were found. Ensure that you configured the clients correctly in the application.yaml file." );
204+ }
205+ // GPT-4o-mini doesn't work too well with the file system tool, so we use 4o here
206+ val options = new OrchestrationChatOptions (config .withLlmConfig (GPT_4O ));
207+ options .setToolCallbacks (List .of (toolCallbackProvider .getToolCallbacks ()));
208+ options .setInternalToolExecutionEnabled (true );
209+
210+ val sys =
211+ new SystemMessage (
212+ """
213+ Please read through the markdown files in my file system.
214+ Ensure to first query the allowed directories.
215+ Then use any `.md` files you find to answer the user's question.
216+ Do **NOT** query for `*.md` since that doesn't work, ensure to query for `.md` instead.""" );
217+ val usr = new UserMessage ("How can I use Spring AI with the SAP AI SDK?" );
218+
219+ val prompt = new Prompt (List .of (sys , usr ), options );
220+ return client .call (prompt );
221+ }
222+
175223 /**
176224 * Chat request to OpenAI through the Orchestration service using chat memory.
177225 *
0 commit comments