Your Spring Boot application is terminating quickly because you haven't defined a main application class with the @SpringBootApplication annotation and a main function.
The DatasetTools class correctly defines a Spring AI tool, but it doesn't serve as the entry point for your Spring Boot application. Spring needs a designated class to bootstrap the application context, scan components, and start the embedded web server (like Tomcat or Netty, which would listen on port 8080).
Here's how to fix it:
Step 1: Create Your Main Spring Boot Application Class
You need a class annotated with @SpringBootApplication and a main function that calls SpringApplication.run.
Create a new file (e.g., ErpMcpServerApplication.kt) in a parent package relative to de.mait.ai.erp_mcp_server.tool (or in the same package):
package de.mait.ai.erp_mcp_server // Consider putting it in a parent package for better component scanning
import org.springframework.boot.autoconfigure.SpringBootApplication
import org.springframework.boot.runApplication
@SpringBootApplication
class ErpMcpServerApplication // You can name this class anything relevant to your project
fun main(args: Array<String>) {
runApplication<ErpMcpServerApplication>(*args)
}
Step 2: Ensure Necessary Dependencies
For a web server on port 8080 and Spring AI capabilities, you'll need specific dependencies in your build.gradle.kts (or pom.xml if you're using Maven).
For Gradle (build.gradle.kts):
plugins {
// ... other plugins like 'kotlin-spring', 'jvm'
id("org.springframework.boot") version "3.2.5" // Use the latest stable version
id("io.spring.dependency-management") version "1.1.4"
}
group = "de.mait.ai"
version = "0.0.1-SNAPSHOT"
java {
sourceCompatibility = JavaVersion.VERSION_17 // Or your desired Java version
}
repositories {
mavenCentral()
// If using Spring Milestones/Snapshots for Spring AI, add:
// maven { url = uri("https://repo.spring.io/milestone") }
// maven { url = uri("https://repo.spring.io/snapshot") }
}
dependencies {
// Spring Boot Web Starter for the embedded web server and REST capabilities
implementation("org.springframework.boot:spring-boot-starter-web")
// Spring AI Starter (choose your preferred model, e.g., OpenAI, Ollama)
// Example for OpenAI (if you're using it):
// implementation("org.springframework.ai:spring-ai-openai-spring-boot-starter")
// OR for a local LLM like Ollama:
// implementation("org.springframework.ai:spring-ai-ollama-spring-boot-starter")
// Core Spring AI dependency for @Tool annotation recognition
implementation("org.springframework.ai:spring-ai-core")
// Kotlin support for Spring Boot
implementation("org.springframework.boot:spring-boot-starter-validation")
implementation("com.fasterxml.jackson.module:jackson-module-kotlin")
implementation("org.jetbrains.kotlin:kotlin-reflect")
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8")
// Test dependencies
testImplementation("org.springframework.boot:spring-boot-starter-test")
testImplementation("org.springframework.ai:spring-ai-test") // For Spring AI testing utilities
}
tasks.withType<org.jetbrains.kotlin.gradle.tasks.KotlinCompile> {
kotlinOptions {
freeCompilerArgs += "-Xjsr305=strict"
jvmTarget = "17" // Must match sourceCompatibility
}
}
Step 3: (Optional but Recommended) Create an AI Controller
Your DatasetTools class defines a tool. To use this tool, you'll typically have an AI ChatModel and a REST controller that interacts with it, possibly exposing an SSE endpoint for streaming AI responses.
Here's an example of how you might set up a basic controller to use your tool:
package de.mait.ai.erp_mcp_server.controller
import de.mait.ai.erp_mcp_server.tool.DatasetTools
import org.springframework.ai.chat.ChatClient
import org.springframework.ai.chat.Generation
import org.springframework.ai.chat.messages.UserMessage
import org.springframework.ai.chat.prompt.Prompt
import org.springframework.ai.chat.prompt.PromptTemplate
import org.springframework.ai.chat.prompt.SystemPromptTemplate
import org.springframework.ai.model.function.FunctionCallingTool
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Bean
import org.springframework.context.annotation.Configuration
import org.springframework.http.MediaType
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux
import reactor.core.scheduler.Schedulers
@RestController
class AiAssistantController(
private val chatClient: ChatClient,
private val datasetTools: DatasetTools // Inject your tool class
) {
// Example endpoint to use the tool
@GetMapping("/api/chat-with-tool")
fun chatWithTool(@RequestParam message: String): String {
val userMessage = UserMessage(message)
val prompt = Prompt(userMessage)
// The chatClient will automatically discover and use tools marked with @Tool
// if they are available in the application context and the model supports function calling.
val response = chatClient.call(prompt)
return response.result.output.content
}
// Example for an SSE endpoint if you want to stream AI responses
@GetMapping("/api/chat-stream", produces = [MediaType.TEXT_EVENT_STREAM_VALUE])
fun chatStream(@RequestParam message: String): Flux<String> {
val promptText = "Respond to this message: $message"
val promptTemplate = PromptTemplate(promptText)
val prompt = promptTemplate.create()
return chatClient.stream(prompt)
.map { generation: Generation -> generation.output.content }
.doOnNext { content -> println("Streamed: $content") }
.doOnError { throwable -> System.err.println("Error during streaming: ${throwable.message}") }
.subscribeOn(Schedulers.boundedElastic()) // Use a suitable scheduler for non-blocking operations
}
}
// You might need a configuration class to define your ChatClient bean
// if you're not using a spring-boot-starter-ai that auto-configures it
@Configuration
class SpringAiConfig {
@Bean
fun datasetTool(): FunctionCallingTool {
return FunctionCallingTool.builder(DatasetTools::class.java)
.withName("erp_create_dataset")
.withDescription("Erstellt einen neuen Kunden innerhalb von abas-ERP")
.build()
}
// You also need to configure your ChatModel, e.g., for OpenAI
// You would inject the ChatClient here or rely on auto-configuration
// For example, if using Ollama:
// @Bean
// fun ollamaChatModel(@Value("\${spring.ai.ollama.base-url}") baseUrl: String): ChatModel {
// return OllamaChatModel(
// OllamaOptions.builder().withModel("llama2").build(),
// baseUrl
// )
// }
}
Important Notes for DatasetTools:
- You have
@Component and @Service on DatasetTools. While @Service is itself a @Component, having both is redundant. You only need one, e.g., @Component or @Service. @Component is perfectly fine here.
- Spring AI's
@Tool annotation works by scanning beans in the application context. As long as DatasetTools is a Spring-managed component (which it is with @Component), Spring AI will find its @Tool methods and make them available to function-calling capable models (like OpenAI's GPT-3.5/4, Ollama with certain models, etc.).
- The
DatasetTools method createDataset currently returns a hardcoded string. In a real application, this would interact with your ERP system (e.g., call a service, save to a database, etc.) and return a meaningful result from that operation.
After making these changes, when you start your Spring Boot application (e.g., by running the main function or using ./gradlew bootRun), it should start up successfully, listen on port 8080, and your /api/chat-with-tool or /api/chat-stream endpoints (if you created them) will be available.