spring-ai-mcp-example

cmsong111/spring-ai-mcp-example

3.1

If you are the rightful owner of spring-ai-mcp-example and would like to certify it and/or have it hosted online, please leave a comment on the right or send an email to henry@mcphub.com.

This document provides a comprehensive overview of a Spring AI MCP Server Example using STDIO, detailing its core functionalities, features, and usage.

Spring AI MCP Server Example (STDIO)

스프링 AI MCP 서버 예제 프로젝트

클로드 세팅

{
    "mcpServers": {
        "article-server": {
            "type": "stdio",
            "command": "java",
            "args": [
                "-jar",
                "./spring-mcp-example-0.0.1-SNAPSHOT.jar"
            ]
        }
    }
}

클로드 캡쳐

도구 리스트

게시글 작성 및 게시글 수정 도구

게시글 리스트 조회 및 삭제

삭제 확인

핵심 코드

import org.springframework.ai.tool.annotation.Tool
import org.springframework.stereotype.Component

@Component
class ArticleService(
    private val articleRepository: ArticleRepository
) {

    @Tool(description = "새로운 아티클(게시글)을 생성합니다.")
    fun createArticle(articleCreateForm: ArticleCreateForm): Article {
        return articleRepository.save(
            Article.create(
                title = articleCreateForm.title,
                content = articleCreateForm.content,
                author = articleCreateForm.author,
            )
        )
    }

    @Tool(description = "아이디로 아티클을 조회합니다. 존재하지 않으면 null을 반환합니다.")
    fun getArticle(id: Long): Article? {
        return articleRepository.findById(id).orElse(null)
    }

    @Tool(description = "모든 아티클을 목록으로 반환합니다.")
    fun listArticles(): List<Article> {
        return articleRepository.findAll()
    }

    @Tool(description = "아티클을 수정합니다. id로 기존 아티클을 찾아 title/content를 갱신합니다.")
    fun updateArticle(form: ArticleUpdateForm): Article {
        val article = articleRepository.findById(form.id)
            .orElseThrow { IllegalArgumentException("Article with id=${form.id} not found") }

        article.update(
            title = form.title,
            content = form.content,
        )

        return articleRepository.save(article)
    }

    @Tool(description = "아티클을 삭제합니다. 성공하면 true를 반환합니다.")
    fun deleteArticle(id: Long): Boolean {
        if (!articleRepository.existsById(id)) return false
        articleRepository.deleteById(id)
        return true
    }
}