-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpringAIExample.java
More file actions
195 lines (173 loc) · 8.86 KB
/
SpringAIExample.java
File metadata and controls
195 lines (173 loc) · 8.86 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package dev.arcade.example.springai;
import dev.arcade.client.ArcadeClient;
import dev.arcade.models.AuthorizationResponse;
import dev.arcade.models.tools.AuthorizeToolRequest;
import dev.arcade.models.tools.ExecuteToolRequest;
import dev.arcade.models.tools.ExecuteToolResponse;
import java.util.Map;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Service;
@SpringBootApplication
public class SpringAIExample {
private static final String SYSTEM_PROMPT = """
You are a specialized Music Assistant with access to two MCP tools: get_spotify_state and play_song.
Your goal is to provide a seamless, proactive audio experience. Follow these operational guidelines:
Context Awareness: Before playing any music, always use get_spotify_state to see if music is already playing. If it is, acknowledge what is currently playing before switching to the new track.
Proactive Assistance: If the user asks for a song and Spotify is currently paused, inform the user you are resuming playback or starting the session for them.
Error Handling: If a song fails to play or the tool returns an error, check the state again to see if the player is disconnected or if the track simply wasn't found.
Tone: Be concise, upbeat, and music-focused.
Chain of Thought: When a user asks for a song, your internal logic should be: Check State -> Inform User -> Execute Play.
""";
public static void main(String[] args) {
SpringApplication.run(SpringAIExample.class, args);
}
@Bean
ApplicationRunner runner(ChatClient.Builder chatClientBuilder, ArcadeToolProvider arcadeToolProvider) {
return args -> {
ChatClient chatClient = chatClientBuilder
.defaultSystem(SYSTEM_PROMPT)
.defaultTools(arcadeToolProvider) // see below, each tool is annotated with @Tool
.build();
String summary = chatClient
.prompt()
.user("If my music is not currently playing, play something to get me in the mood to write code.")
.call()
.content();
System.out.println(summary);
};
}
/**
* Exposes Arcade Tools to Spring AI.
*/
@Service
public static class ArcadeToolProvider {
private final Logger log = LoggerFactory.getLogger(ArcadeToolProvider.class);
private final ArcadeClient client;
private final String userId;
ArcadeToolProvider(ArcadeClient client, @Value("${arcade.user-id}") String userId) {
this.client = client;
this.userId = userId;
}
/**
* Exposes an Arcade Tool call to Spotify.GetPlaybackState, using Spring AI annotations.
*
* @return A string object of the playback state.
*/
@Tool(
name = "play_song",
description = "Plays a song by an artist and queues four more songs by the same artist")
String play(@ToolParam(description = "The name of the artist to play") String name) {
return executeTool("Spotify.PlayArtistByName", Map.of("name", name));
}
/**
* Exposes an Arcade Tool call to Spotify.GetPlaybackState, using Spring AI annotations.
*
* @return A string object of the playback state.
*/
@Tool(name = "get_spotify_state", description = """
Get information about the user's current playback state,
including track or episode, and active device.
This tool does not perform any actions. Use other tools to control playback.
""")
String playbackState() {
return executeTool("Spotify.GetPlaybackState", Map.of());
}
/**
* Executes the specified tool with the provided input. Handles authorization and errors.
*
* @param toolName the name of the tool to be executed
* @param input the input parameters required for tool execution
* @return the result of the tool execution as a string; the result may include
* the output of the tool, an error message, or an authorization requirement
*/
private String executeTool(String toolName, Map<String, Object> input) {
log.debug("Executing tool {}, with input: {}", toolName, input);
try {
ExecuteToolResponse response = client.tools()
.execute(ExecuteToolRequest.builder()
.toolName(toolName)
.userId(userId)
.input(input)
.build());
log.debug(
"Tool {} executed, with a status of '{}'",
toolName,
response.status().orElse(null));
// process the result
if (response.success().orElse(false)) {
String result =
response.output().map(o -> o._value().toString()).orElse("{}");
log.debug("Tool {} returned: {}", toolName, result);
return result;
}
Optional<ExecuteToolResponse.Output.Error> error =
response.output().flatMap(ExecuteToolResponse.Output::error);
if (error.isPresent()) {
String errorMessage = error.get().message();
if (errorMessage.contains("authorization required")) {
AuthorizationResult auth = requestAuthorization(toolName);
if (auth.requiresAction()) {
log.debug("Tool {} requires authorization, open a browser to {}", toolName, auth.url());
return String.format(
"The '%s' tool requires authorization, open a browser to %s to continue.",
toolName, auth.url());
}
}
log.warn("Tool {} returned an error: {}", toolName, errorMessage);
return "Error: " + errorMessage;
}
return "Error: Tool execution failed";
} catch (Exception e) {
String message = e.getMessage();
if (message != null && message.contains("authorization")) {
AuthorizationResult auth = requestAuthorization(toolName);
if (auth.requiresAction()) {
log.debug("Tool {} requires authorization, open a browser to {}", toolName, auth.url());
return String.format(
"The '%s' tool requires authorization, open a browser to %s to continue.",
toolName, auth.url());
}
}
log.error("Tool execution failed for {}: {}", toolName, message);
return "Error: " + message;
}
}
/**
* Requests authorization for a tool and returns the OAuth URL.
*/
public AuthorizationResult requestAuthorization(String toolName) {
try {
AuthorizationResponse response = client.tools()
.authorize(AuthorizeToolRequest.builder()
.toolName(toolName)
.userId(userId)
.build());
Optional<String> url = response.url();
String statusValue =
response.status().map(s -> s.value().name()).orElse("unknown");
if ("PENDING".equalsIgnoreCase(statusValue) && url.isPresent()) {
return new AuthorizationResult(toolName, url.get(), statusValue);
}
return new AuthorizationResult(toolName, null, statusValue);
} catch (Exception e) {
log.error("Failed to request authorization for {}: {}", toolName, e.getMessage());
return new AuthorizationResult(toolName, null, "error: " + e.getMessage());
}
}
public record AuthorizationResult(String toolName, String url, String status) {
public boolean requiresAction() {
return url != null && "PENDING".equalsIgnoreCase(status);
}
}
}
}