DEV Community

Cover image for Building Cross-Protocol AI Agents with Spring Boot: A2A and MCP Server Guide
vishalmysore
vishalmysore

Posted on

Building Cross-Protocol AI Agents with Spring Boot: A2A and MCP Server Guide

This guide will walk you through creating a Spring Boot application that supports both A2A (Agent-to-Agent) and MCP (Model Context Protocol) protocols. You'll learn how to build an AI agent that can communicate using both protocols and implement security features.

Prerequisites

  • Java 8 or higher
  • Maven
  • Basic knowledge of Spring Boot
  • IDE (VS Code, IntelliJ, or Eclipse)

Step 1: Create a Spring Boot Project

First, create a new Spring Boot project using your preferred method (Spring Initializr or IDE). Then, add the following dependencies to your pom.xml:

<!-- Tools4AI dependencies -->
<dependency>
    <groupId>io.github.vishalmysore</groupId>
    <artifactId>a2ajava</artifactId>
    <version>0.1.8.2</version>
</dependency>
<dependency>
    <groupId>io.github.vishalmysore</groupId>
    <artifactId>tools4ai-annotations</artifactId>
    <version>0.0.2</version>
</dependency>
<dependency>
    <groupId>io.github.vishalmysore</groupId>
    <artifactId>tools4ai-security</artifactId>
    <version>0.0.3</version>
</dependency>
<dependency>
    <groupId>io.github.vishalmysore</groupId>
    <artifactId>tools4ai</artifactId>
    <version>1.1.5</version>
    <exclusions>
        <exclusion>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-core</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.swagger.core.v3</groupId>
            <artifactId>swagger-core-jakarta</artifactId>
        </exclusion>
        <exclusion>
            <groupId>io.swagger.parser.v3</groupId>
            <artifactId>swagger-parser</artifactId>
        </exclusion>
    </exclusions>
</dependency>
Enter fullscreen mode Exit fullscreen mode

Step 2: Configure Your Application

Basic Configuration

Create or update your main application class with the required annotations:

@SpringBootApplication
@EnableAgent                  // Enables A2A/MCP agent functionality
@EnableAgentSecurity         // Optional: Enables security features
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}
Enter fullscreen mode Exit fullscreen mode

Security Configuration (Optional)

If you enabled security with @EnableAgentSecurity, create a security configuration class:

@Configuration
@EnableWebSecurity
@EnableMethodSecurity
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http
            .csrf(csrf -> csrf.disable())
            .headers(headers -> headers
                .frameOptions(frame -> frame.sameOrigin())
            )
            .authorizeHttpRequests(auth -> auth
                .requestMatchers("/public/**", "/swagger-ui/**", 
                               "/v3/api-docs/**", "/.well-known/agent.json", 
                               "/", "/index.html").permitAll()
                .anyRequest().authenticated()
            )
            .httpBasic(Customizer.withDefaults());

        return http.build();
    }

    @Bean
    public UserDetailsService userDetailsService() {
        UserDetails user = User.builder()
            .username("user")
            .password(passwordEncoder().encode("password"))
            .roles("USER")
            .build();

        UserDetails admin = User.builder()
            .username("admin")
            .password(passwordEncoder().encode("admin"))
            .roles("ADMIN")
            .build();

        return new InMemoryUserDetailsManager(user, admin);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 3: Create Your Agent Service

Create a service class with your agent's functionality:

@Log
@Service
@Agent(groupName = "Your Agent Group Name")
public class YourAgentService {

    @PreAuthorize("hasRole('USER')")    // Optional: Add security
    @Action(description = "Description of what this action does")
    public String yourAction(String param) {
        // Your action implementation
        return "Action result";
    }
}
Enter fullscreen mode Exit fullscreen mode

Step 4: Configure Properties

Create application.properties:

spring.application.name=your-agent-name
server.port=7860
a2a.persistence=cache
Enter fullscreen mode Exit fullscreen mode

Step 5: Testing Your Agent

A2A Protocol Testing

Access your agent's A2A card:

curl http://localhost:7860/.well-known/agent.json
Enter fullscreen mode Exit fullscreen mode

Test with authentication:

curl -u user:password http://localhost:7860/.well-known/agent.json
Enter fullscreen mode Exit fullscreen mode

MCP Protocol Testing

List available tools:

curl -H "Content-Type: application/json" \
    -d '{"jsonrpc":"2.0","method":"tools/list","params":{},"id":1}' \
    http://localhost:7860/
Enter fullscreen mode Exit fullscreen mode

Call a tool:

curl -u admin:admin -H "Content-Type: application/json" \
    -d '{
        "jsonrpc": "2.0",
        "method": "tools/call",
        "params": {
            "name": "yourAction",
            "arguments": {
                "param": "value"
            }
        },
        "id": 1
    }' \
    http://localhost:7860/
Enter fullscreen mode Exit fullscreen mode

Step 6: MCP Connector Integration

To connect your server with MCP clients, you can use the mcp-connector. Add this to your MCP configuration:

{
  "myagent": {
    "command": "java",
    "args": [
      "-jar",
      "/path/to/mcp-connector-full.jar",
      "http://localhost:7860/"
    ],
    "timeout": 30000
  }
}
Enter fullscreen mode Exit fullscreen mode

For more details about the MCP connector, visit: https://github.com/vishalmysore/mcp-connector

Advanced Topics

  • OAuth2 integration
  • Custom authentication providers
  • Advanced action handling
  • Callback mechanisms
  • File handling and resource management

These topics will be covered in Part 2 of this guide.

Conclusion

You now have a working A2A/MCP agent server with:

  • Protocol support for both A2A and MCP
  • Security implementation (if enabled)
  • Basic action handling
  • Ready for extension with your custom functionality

Next Steps

  • Add your custom actions
  • Implement more complex security
  • Add database integration
  • Implement file handling
  • Add custom protocols

For more examples and detailed implementation, check out the full project at: https://github.com/vishalmysore/SqlAIAgent

Top comments (0)