Spring Framework integration for Model Context Protocol (MCP), providing Spring AI function calling capabilities and Spring-friendly abstractions for MCP clients and MCP servers
Spring AI MCP provides AOT (Ahead-of-Time) compilation support for GraalVM native images through runtime hints registration. This ensures that MCP schema classes and reflection-based operations work correctly in native images.
import org.springframework.ai.mcp.aot.McpHints;
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;public class McpHints implements RuntimeHintsRegistrar {
void registerHints(RuntimeHints hints, ClassLoader classLoader);
}Runtime hints registrar that registers necessary reflection and resource hints for MCP schema classes to enable GraalVM native image compilation.
The McpHints class automatically registers runtime hints for:
io.modelcontextprotocol.spec.McpSchemaSpring Boot automatically discovers and applies McpHints when building native images. No manual configuration is required when using Spring Boot's native image support.
<dependencies>
<dependency>
<groupId>org.springframework.ai</groupId>
<artifactId>spring-ai-mcp</artifactId>
<version>1.1.2</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.graalvm.buildtools</groupId>
<artifactId>native-maven-plugin</artifactId>
</plugin>
</plugins>
</build>dependencies {
implementation 'org.springframework.ai:spring-ai-mcp:1.1.2'
}
graalvmNative {
binaries {
main {
// Native image configuration
}
}
}# Build native image
mvn -Pnative native:compile
# Run native image
./target/my-app# Build native image
./gradlew nativeCompile
# Run native image
./build/native/nativeCompile/my-appThe McpHints class registers runtime hints for the following MCP schema components:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.ai.mcp.SyncMcpToolCallbackProvider;
@SpringBootApplication
public class NativeImageTestApplication {
public static void main(String[] args) {
SpringApplication.run(NativeImageTestApplication.class, args);
}
@Bean
public SyncMcpToolCallbackProvider mcpProvider(McpSyncClient client) {
return SyncMcpToolCallbackProvider.builder()
.mcpClients(client)
.build();
}
}Compile and run as native image to verify MCP functionality works correctly.
Issue: ClassNotFoundException for MCP schema classes
Solution: Ensure spring-ai-mcp is on the classpath during native image build. The McpHints class should be automatically discovered.
Issue: JSON serialization errors for MCP types
Solution: Verify that McpHints is being applied. Check native image build logs for hint registration.
Issue: Reflection errors when calling MCP methods
Solution: Check that all MCP SDK dependencies are included in the native image build configuration.
If automatic registration doesn't work, you can manually register hints:
import org.springframework.aot.hint.RuntimeHints;
import org.springframework.aot.hint.RuntimeHintsRegistrar;
import org.springframework.context.annotation.ImportRuntimeHints;
@ImportRuntimeHints(CustomMcpHints.class)
@Configuration
public class McpConfiguration {
// Your configuration
}
class CustomMcpHints implements RuntimeHintsRegistrar {
@Override
public void registerHints(RuntimeHints hints, ClassLoader classLoader) {
// Register additional hints if needed
McpHints mcpHints = new McpHints();
mcpHints.registerHints(hints, classLoader);
// Add custom hints here if needed
}
}Native images provide:
Trade-offs: