Skip to content

aws-samples/sample-once-upon-spring-ai

Repository files navigation

Once Upon Spring AI: A Developer's Epic Journey into Agentic Java

Header Image

"Roll for Initiative... in Java!"

Welcome, brave adventurer, to the ultimate Spring AI quest! This comprehensive workshop will transform you from a coding apprentice into a master of AI agent orchestration using Java 25 and Spring AI. Through five epic chapters, you'll learn to create, equip, and command digital companions that can think, act, and collaborate like a legendary adventuring party.

๐ŸŒ The Complete Adventure Map

Your journey through the realms of AI agents is carefully structured as a progressive quest. Each chapter builds upon the previous one - complete them in order to unlock the full power of Spring AI!

๐Ÿง™โ€โ™‚๏ธ Chapter 1: The Art of Agent Summoning

Master the fundamental ritual of agent creation

  • Learn what Spring AI is and how it works
  • Summon your first AI companion โ€” a Dungeon Master chatbot
  • Configure Amazon Bedrock models and system prompts
  • Understand the core concepts of agent development

Equip your agents with community-powered tools

  • Discover Spring AI community tools (spring-ai-agent-utils)
  • Learn how agents autonomously choose and use tools
  • Master web scraping and information gathering with SmartWebFetchTool
  • Understand tool registration and the agentic loop

Forge your own custom tools and enchantments

  • Transform Java methods into agent tools with @Tool and @ToolParam
  • Create the legendary Dice of Destiny
  • Master the //SOURCES directive for multi-file JBang projects
  • Build domain-specific capabilities using Java 25 records

Expose tools as remote services through Model Context Protocol

  • Build and deploy an MCP server with Spring Boot and @McpTool
  • Create an MCP client with an interactive REPL
  • Understand distributed tool architectures over Streamable HTTP
  • Master external service connections using SyncMcpToolCallbackProvider

Command multiple agents in perfect harmony

  • Build a complete multi-agent D&D system with character inventory management
  • Master Agent-to-Agent (A2A) communication with AgentCard and AgentExecutor
  • Orchestrate specialized agents (Rules, Character, Game Master) working together
  • Implement conversation memory with MessageChatMemoryAdvisor
  • Combine A2A, MCP, and RAG in a single architecture

๐Ÿ—บ๏ธ Choose Your Path

Before you start, commit to one of the two paths below. Both are first-class โ€” pick the one that fits how you want to work.

๐Ÿง™ The JBang Path

For: Java developers curious to try a new way of running Java, and non-Java developers who want a minimal setup.

Tooling: JBang CLI + any text editor (VS Code, Sublime, nano โ€” whatever you like).

You'll run code like this: jbang DungeonMasterSimple.java

The contract: Follow the workshop instructions exactly as written. If you deviate, you're on your own.


โš”๏ธ The IntelliJ Path

For: Java developers who want to work the way they usually do.

Tooling: IntelliJ IDEA + Maven. No JBang needed.

Prerequisite: You already have IntelliJ IDEA for Java installed on your laptop (Community or Ultimate). This path does not cover installing the IDE.

You'll run code like this: open chapter1-maven/ in IntelliJ, click the green arrow next to main() in DungeonMasterSimple.java.

The contract: Work the way you normally do. If your IntelliJ / Maven / JDK setup has quirks, you're on your own.

โš ๏ธ Pick one and stick to it. Mixing paths (editing the JBang file while running the Maven project, or vice versa) is the fastest way to get confused.

๐ŸŽ’ Preparing for Your Quest

Essential Gear (Prerequisites)

Before embarking on this legendary adventure, ensure you have:

  1. Java 25 as your trusty spellcasting focus.

๐Ÿง™ If you picked the JBang Path

Install JBang

JBang runs .java files directly with dependencies declared as //DEPS comments โ€” no Maven, no Gradle, no pom.xml.

curl -Ls https://sh.jbang.dev | bash -s - app setup
jbang --version

Install Java 25 via JBang

jbang jdk install 25
jbang jdk default 25
eval $(jbang jdk java-env)
java -version   # Should show: openjdk version "25"

To make this permanent, add eval $(jbang jdk java-env) to your ~/.bashrc or ~/.zshrc.

โš”๏ธ If you picked the IntelliJ Path

Install Java 25

Install JDK 25 the way you normally do โ€” Amazon Corretto, Homebrew (brew install openjdk@25), or your usual JDK manager. Verify:

java -version   # Should show: openjdk version "25"

Amazon Bedrock API Key โ€” this content uses Amazon Bedrock so make sure to get your API key to get started

  1. Sign in to the Amazon Bedrock Console
  2. In the left navigation pane, select API keys
  3. Click the Short-term API keys tab, then choose Generate short-term API key
  4. Copy the generated API key and set it as an environment variable:
export AWS_BEARER_TOKEN_BEDROCK=<your-api-key>

๐Ÿ’ก Tip: A plain export only lives in the current terminal. If you'd like every new shell, IDE run-config, and tool to inherit it, add the line to ~/.zshenv (zsh) or ~/.bash_profile (bash) instead. This avoids the classic "why isn't my agent answering?" surprise when you open a new terminal mid-workshop.

A sense of adventure and willingness to experiment! ๐ŸŽฒ

โœ… Verify your setup

Before running Chapter 1, paste this into your terminal โ€” it gives a one-glance health check.

java -version
echo "Bedrock:  ${AWS_BEARER_TOKEN_BEDROCK:+OK (length=${#AWS_BEARER_TOKEN_BEDROCK})}${AWS_BEARER_TOKEN_BEDROCK:-MISSING โ€” see step 3}"

You should see openjdk version "25" line and Bedrock: OK (length=NNNN).

If Bedrock says MISSING, revisit Amazon Bedrock API Key step above.

๐ŸŽฏ How to Embark on Your Quest

The Sacred Order of Learning

โš ๏ธ REMINDER: Complete the chapters in order! Each builds upon the previous one's knowledge and skills.

  1. Progress through each chapter - Don't skip ahead, each chapter introduces essential concepts
  2. Complete all TODOs - Each chapter has guided exercises to master the concepts
  3. Test your creations - Run your agents with jbang and see them come to life
  4. Experiment and explore - Try variations and push the boundaries

Workshop Structure

Each chapter follows the same magical pattern:

  • ๐Ÿ“œ README Guide: Complete instructions and background lore
  • ๐ŸŽฏ TODO Exercises: Hands-on coding challenges to complete
  • ๐Ÿงช Testing Instructions: How to verify your magical creations work
  • ๐Ÿ† Solution Reference: Complete working examples in the solution branch

๐Ÿง™โ€โ™‚๏ธ What is Spring AI?

Spring AI is a powerful framework for creating AI-powered applications in Java - think of it as your spellbook for summoning digital companions that can interact with tools and services. Like a well-equipped adventuring party, Spring AI provides:

  • ๐ŸŽญ Agent Creation: Easy summoning via ChatClient โ€” the gateway to AI conversations
  • โš”๏ธ Tool Integration: Built-in @Tool / @ToolParam annotations and community tools
  • ๐Ÿ”„ Model Flexibility: Support for multiple AI providers (Amazon Bedrock, OpenAI, Ollama, and more)
  • ๐ŸŒ External Connections: Integration with services through MCP (Model Context Protocol)
  • ๐Ÿฐ Multi-Agent Systems: Coordinate multiple agents using A2A (Agent-to-Agent) protocol

๐Ÿ“– The Sacred Terminology

  • ๐Ÿค– ChatClient: The central abstraction for interacting with an AI model in Spring AI
  • ๐Ÿ”ง @Tool: Annotation that transforms a Java method into a callable AI function
  • ๐Ÿ“‹ System Prompt: The character sheet defining your agent's personality and behavior
  • ๐Ÿง  ChatModel: The bridge to the AI provider (Bedrock, OpenAI, etc.)
  • ๐ŸŒ MCP: Model Context Protocol for connecting to external tool servers
  • ๐Ÿฐ A2A: Agent-to-Agent protocol for multi-agent communication
  • ๐ŸŽ’ JBang: Build tool that runs .java files directly with embedded //DEPS metadata

๐Ÿ—‚๏ธ Project Structure

The repo ships two parallel views of the same content โ€” pick the tree that matches your path.

๐Ÿง™ JBang Path โ€” single-file Java sources, run directly with jbang

sample-once-upon-spring-ai/
โ”œโ”€โ”€ README.md
โ”œโ”€โ”€ chapter1/                          # ๐Ÿง™โ€โ™‚๏ธ The Art of Agent Summoning
โ”‚   โ””โ”€โ”€ DungeonMasterSimple.java
โ”œโ”€โ”€ chapter2/                          # โš”๏ธ AI Agent with Built-in Tools
โ”‚   โ””โ”€โ”€ DungeonMasterWithBuiltInTools.java
โ”œโ”€โ”€ chapter3/                          # ๐Ÿ”จ The Adventurer's Arsenal
โ”‚   โ”œโ”€โ”€ DiceTools.java
โ”‚   โ””โ”€โ”€ DungeonMasterWithCustomTools.java
โ”œโ”€โ”€ chapter4/                          # ๐ŸŒ The Tavern Notice Board (MCP)
โ”‚   โ”œโ”€โ”€ DiceRollMcpServer.java
โ”‚   โ”œโ”€โ”€ DungeonMasterMCPClient.java
โ”‚   โ””โ”€โ”€ application.properties
โ””โ”€โ”€ chapter5/                          # ๐Ÿฐ The Council of Agents (A2A)
    โ”œโ”€โ”€ agents/
    โ”‚   โ”œโ”€โ”€ rules/
    โ”‚   โ”‚   โ”œโ”€โ”€ RulesAgent.java                 # D&D rules lookup agent with RAG
    โ”‚   โ”‚   โ””โ”€โ”€ RulesTools.java                 # PDF knowledge base search tools
    โ”‚   โ”œโ”€โ”€ character/
    โ”‚   โ”‚   โ”œโ”€โ”€ CharacterAgent.java             # Character management agent
    โ”‚   โ”‚   โ”œโ”€โ”€ CharacterTools.java             # Character CRUD & inventory tools
    โ”‚   โ”‚   โ””โ”€โ”€ characters.json                 # Persistent character storage
    โ”‚   โ””โ”€โ”€ gamemaster/
    โ”‚       โ”œโ”€โ”€ GameMasterOrchestrator.java     # Spring Boot app with A2A + MCP
    โ”‚       โ”œโ”€โ”€ GameMasterService.java          # Agent discovery & orchestration
    โ”‚       โ””โ”€โ”€ GameMasterController.java       # REST API endpoints
    โ”œโ”€โ”€ test/
    โ”‚   โ””โ”€โ”€ test.http                           # HTTP test requests
    โ””โ”€โ”€ utils/
        โ””โ”€โ”€ CreateKnowledgeBase.java            # PDF โ†’ vector store ingestion

โš”๏ธ IntelliJ Path โ€” Maven mirrors, open in IntelliJ and run from main()

sample-once-upon-spring-ai/
โ”œโ”€โ”€ chapter1-maven/                    # Mirror of chapter1 โ€” runs DungeonMasterSimple
โ”œโ”€โ”€ chapter2-maven/                    # Mirror of chapter2 โ€” runs DungeonMasterWithBuiltInTools
โ”œโ”€โ”€ chapter3-maven/                    # Mirror of chapter3 โ€” runs DungeonMasterWithCustomTools
โ”œโ”€โ”€ chapter4-maven-server/             # MCP server (DiceRollMcpServer + application.properties)
โ”œโ”€โ”€ chapter4-maven-client/             # MCP client (DungeonMasterMCPClient)
โ””โ”€โ”€ chapter5-maven/                    # Multi-module Maven project for the A2A council
    โ”œโ”€โ”€ rules/                         # โ†’ RulesAgent, RulesTools
    โ”œโ”€โ”€ character/                     # โ†’ CharacterAgent, CharacterTools, characters.json
    โ”œโ”€โ”€ gamemaster/                    # โ†’ GameMasterOrchestrator, Service, Controller
    โ””โ”€โ”€ utils/                         # โ†’ CreateKnowledgeBase

๐Ÿ’ก Chapter 4 is split into two Maven projects (-server and -client) because MCP requires the server and client to run as independent processes โ€” each gets its own pom.xml and IntelliJ run config. Chapter 5 is a single multi-module project so the four agents share dependencies and can be launched from one IntelliJ window.

โ˜๏ธ Amazon Bedrock Models

You must have permissions to Amazon Bedrock in an AWS account. You can use any model available in your Bedrock console โ€” simply update the model ID in each chapter's source file to match your preferred model.

This content default to:

  • ๐ŸŽญ Anthropic Claude (via Amazon Bedrock) โ€” All chapters
  • ๐Ÿงฎ Amazon Titan Embed Text V2 โ€” Chapter 5 (vector store embeddings)

๐ŸŽ“ Learning Objectives

By completing this workshop, you'll master:

  • โœ… Agent Fundamentals: Create, configure, and deploy AI agents with Spring AI and ChatClient
  • โœ… Tool Mastery: Use community tools and create custom ones with @Tool
  • โœ… External Integration: Connect agents to external services via MCP
  • โœ… Multi-Agent Systems: Build distributed applications with A2A protocol
  • โœ… Modern Java: Leverage Java 25 features โ€” records, unnamed classes, text blocks, var, and JBang

๐Ÿ“š Additional Resources โ€” Continue Your Quest

This workshop is a low-bar, practical intro. Spring AI has many more abstractions than we could cover in five chapters. If you want to go deeper, here's a curated path โ€” grouped so you can dive into whatever caught your interest.

๐Ÿง™โ€โ™‚๏ธ Spring AI โ€” go beyond the intro

  • ๐Ÿ“– Spring AI Reference Documentation โ€” the complete guide
  • ๐Ÿค– ChatClient API โ€” fluent prompts, system prompts, entity mapping
  • ๐Ÿงฉ Advisors API โ€” the interceptor chain behind memory, RAG, and logging (Chapter 5 uses MessageChatMemoryAdvisor)
  • ๐Ÿ”ง Tool Calling โ€” the full @Tool / @ToolParam model from Chapters 3โ€“4
  • ๐Ÿ“š Retrieval-Augmented Generation (RAG) โ€” the patterns behind Chapter 5's rules agent
  • ๐Ÿ—‚๏ธ Vector Stores โ€” swap the in-memory SimpleVectorStore for OpenSearch, pgvector, and more
  • ๐Ÿง  Chat Memory โ€” short-term conversation memory, the foundation for stateful agents
  • ๐Ÿ’ป Spring AI GitHub โ€” source, issues, and release notes
  • ๐ŸŒฑ Spring AI Community โ€” agent-utils (Chapter 2) and the A2A bindings (Chapter 5) live here

โ˜๏ธ Amazon Bedrock

๐ŸŒ Protocols โ€” MCP & A2A

  • ๐Ÿ”Œ Model Context Protocol โ€” the MCP spec (Chapter 4)
  • ๐Ÿ”Œ MCP Java SDK โ€” the io.modelcontextprotocol client/server library you used
  • ๐ŸŒ A2A Protocol โ€” Agent-to-Agent specification (Chapter 5)
  • ๐Ÿ”ง Spring AI MCP โ€” how Spring AI wires MCP into the ChatClient

๐ŸŽ’ Tooling & Modern Java

๐Ÿงญ Going to production

  • ๐Ÿ—๏ธ AWS Agentic AI Patterns โ€” the pattern catalog (Tool-Based, RAG, Memory, Multi-Agentโ€ฆ) this workshop demonstrates
  • ๐Ÿข Amazon Bedrock AgentCore โ€” managed runtime, memory, gateway, and identity for taking these agents to production

๐ŸŽฒ The Adventure Never Ends...

Remember, the most epic adventures are the ones you create yourself. Whether you're building the next great AI application or just exploring the boundaries of what's possible, you now have the tools and knowledge to make it happen.

May your agents be wise, your tools be sharp, and your code compile on the first try! ๐ŸŽฒโœจ


"The best way to predict the future is to build the agents that will create it." - Modern Developer Wisdom

Happy coding, Agent Master! ๐Ÿ‰โš”๏ธ๐Ÿง™โ€โ™‚๏ธ

About

No description, website, or topics provided.

Resources

License

Code of conduct

Contributing

Security policy

Stars

24 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors