examples/multiagent-patterns/supervisor/README.md
This example implements the supervisor pattern with Spring AI Alibaba. A central supervisor agent coordinates specialized agents (calendar and email) by calling them as tools via AgentTool.
Supervisor (main agent)
Receives user requests, decides which specialized agent(s) to call, and synthesizes results. It only sees high-level tools: schedule_event and manage_email.
Calendar agent
Handles scheduling: parses natural language (e.g. "next Tuesday at 2pm"), checks availability, and creates events. Exposed to the supervisor as the tool schedule_event with a single string input (the user's scheduling request).
Email agent
Handles email: composes and sends messages from natural language. Exposed as the tool manage_email with a single string input (the user's email request).
Specialized agents are stateless from the user's perspective; the supervisor keeps the conversation and delegates one-off tasks to them. Each specialized agent runs in a focused context (its own instruction + the request string).
Specialized agents as tools
Calendar and email agents are wrapped with AgentTool.getFunctionToolCallback(agent) so the supervisor invokes them as tools.
Instruction and input type
Each specialized agent has:
input parameter.Tool-per-agent
One tool per specialized agent (schedule_event, manage_email) for clear routing and descriptions.
Stub APIs
Calendar and email "API" calls are stubbed (e.g. in CalendarStubTools, EmailStubTools). In production you would replace these with real calendar/email integrations.
examples/multiagent-patterns/supervisor/
├── README.md
├── pom.xml
└── src/main/
├── java/.../supervisor/
│ ├── SupervisorApplication.java # Spring Boot entry
│ ├── SupervisorConfig.java # Beans: calendarAgent, emailAgent, supervisorAgent
│ ├── SupervisorRunner.java # Optional demo runner (see below)
│ └── tools/
│ ├── CalendarStubTools.java # create_calendar_event, get_available_time_slots
│ └── EmailStubTools.java # send_email
└── resources/
└── application.yml
Set your API key:
export AI_DASHSCOPE_API_KEY=your-dashscope-api-key
From the repo root:
./mvnw -pl :supervisor -am -B package -DskipTests
Or from this directory (if the parent POM is available):
cd examples/multiagent-patterns/supervisor
mvn -B package -DskipTests
Default: the app starts without calling the model (no demo run):
java -jar target/supervisor-0.0.1-SNAPSHOT.jar
# or
./mvnw -pl :supervisor spring-boot:run
To run the two demo scenarios on startup (same as in the reference doc):
Set:
export supervisor.run-examples=true
# or add to application.yml: supervisor.run-examples: true
Then start the app as above. The runner will call the supervisor with these two user messages and log the assistant replies.
Inject the supervisor agent and call it with a user message:
@Qualifier("supervisorAgent")
@Autowired
ReactAgent supervisorAgent;
// Single turn
AssistantMessage response = supervisorAgent.call(new UserMessage("Schedule a meeting tomorrow at 10am"));
String text = response.getText();
You can also use supervisorAgent.call(List<Message> messages) or supervisorAgent.call(Map<String, Object> inputs) for more control over state and history.
spring.ai.dashscope.api-key
Required for the chat model (supervisor and specialized agents). Defaults to AI_DASHSCOPE_API_KEY env var.
supervisor.run-examples
If true, runs the two demo scenarios on startup. Default: false.
schedule_event and manage_email.get_available_time_slots and create_calendar_event, returns a short confirmation text.send_email, returns a short confirmation text.This mirrors the reference example's flow: supervisor routes to specialized agents, each agent runs in isolation with its own instruction and tools, and only the final assistant message is shown to the user.