본문 바로가기
dev/spring

Spring AI MCP with IntelliJ IDEA

by igooo 2025. 5. 26.
728x90

개요

MCP를 지원하는 AI 프로그램들도 많아졌고, 대부분에 AI 기능을 제공하는 개발툴에서도 MCP를 지원하게 되어 간단하게 Spring을 사용하여 MCP 프로그램을 개발하고, InteilliJ IDEA를 사용하여 개발한 MCP를 사용하는 방법을 알아본다.

 

Getting Started

아래 예제에서는 MAC에서 Spring Native 빌드를 사용하여 MCP 서버를 빌드하고, 빌드된 바이너리를 사용하여 IntelliJ IDEA에 연동한다. 윈도에서도 동일하게 사용가능하다.

 

MCP 관련한 예제는 지난 게시글을 확인한다.

https://blog.igooo.org/172

 

Spring AI 스펙은 아래 블로그를 확인한다.

https://spring.io/blog/2025/05/20/spring-ai-1-0-GA-released

 

MCP 서버  개발

프로젝트 생성

Spring initializr를 사용하여 프로젝트를 생성한다.

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.5.0'
    id 'io.spring.dependency-management' version '1.1.7'
    id 'org.graalvm.buildtools.native' version '0.10.6'
}

......

ext {
    set('springAiVersion', "1.0.0")
}

dependencies {
    implementation 'org.springframework.ai:spring-ai-starter-mcp-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.ai:spring-ai-bom:${springAiVersion}"
    }
}

 

Tool 설정

MCP로 사용할 기능을 정의한다. 예제에서는 사용자 목록과 사용자를 저장하는 기능을 만들어 본다. 

(예제에서는 간단하게 list에 데이터를 저장하지만 실제 서비스에서는 DB를 연동한다.

@Component
class UserTools {
    private final List<User> users = new ArrayList<>();

    @Tool(name = "list_users", description = "Return all users")
    List<User> listUsers() {
        // 실제 서비스에서는 DB에서 조회힌다.
        return users;
    }

    @Tool(name = "add_user", description = "Store a single user in the collection.")
    User listUsers(@ToolParam(required = true, description = "The name of the User") String name,
                   @ToolParam(required = true, description = "The age of the User") int age) {
        var user = new User(name, age);
        // 실제 서비스에서는 DB에 저장한다.
        this.users.add(user);
        return user;
    }
}

 

Tool 정보를 Bean으로 등록한다.

@Configuration
class UserToolsConfig {
    @Bean
    ToolCallbackProvider userToolProvier(UserTools userTools) {
        return MethodToolCallbackProvider.builder().toolObjects(userTools).build();
    }
}

 

stdio 모드로 사용하기 위하여 설정을 추가한다.

spring.application.name=spring-mcp
spring.main.banner-mode=off
logging.level.root=off
spring.ai.mcp.server.stdio=true

 

 

Native Build

GraalVM을 사용하여 실행 가능한 파일로 생성한다. (M4 MacBook Pro에서 빌드 시간은 약 1분 정도) 

23:54:06: Executing 'nativeCompile'…

> Task :compileJava
> Task :processResources
> Task :classes
> Task :resolveMainClassName
> Task :processAot
> Task :compileAotJava
> Task :processAotResources UP-TO-DATE
> Task :aotClasses
> Task :jar

> Task :generateResourcesConfigFile
[native-image-plugin] Resources configuration written into /Users/igooo/Documents/workspace/spring-mcp/build/native/generated/generateResourcesConfigFile/resource-config.json

> Task :nativeCompile
[native-image-plugin] GraalVM Toolchain detection is disabled
[native-image-plugin] GraalVM location read from environment variable: JAVA_HOME
[native-image-plugin] Native Image executable path: /Users/igooo/.sdkman/candidates/java/24-graal/lib/svm/bin/native-image
Warning: Using a deprecated option --report-unsupported-elements-at-runtime from 'META-INF/native-image/org.igooo/spring-mcp/native-image.properties' in 'file:///Users/igooo/Documents/workspace/spring-mcp/build/resources/aot/'. The option is deprecated and will be removed in the future. The use of unsupported elements is always reported at run time.
Warning: Option 'DynamicProxyConfigurationResources' is deprecated and might be removed in a future release: This can be caused by a proxy-config.json file in your META-INF directory. Consider including proxy configuration in the reflection section of reachability-metadata.md instead.. Please refer to the GraalVM release notes.
========================================================================================================================
GraalVM Native Image: Generating 'spring-mcp' (executable)...
========================================================================================================================
For detailed information and explanations on the build output, visit:
https://github.com/oracle/graal/blob/master/docs/reference-manual/native-image/BuildOutput.md
------------------------------------------------------------------------------------------------------------------------
[1/8] Initializing...                                                                                    (5.4s @ 0.16GB)
 Java version: 24+36, vendor version: Oracle GraalVM 24+36.1

......
========================================================================================================================
Finished generating 'spring-mcp' in 1m 5s.
[native-image-plugin] Native Image written to: spring-mcp/build/native/nativeCompile

BUILD SUCCESSFUL in 1m 7s
9 actionable tasks: 8 executed, 1 up-to-date
23:55:13: Execution finished 'nativeCompile'.

 

 

 

IntelliJ IDEA에서 MCP 연동하기

MCP 설정하기

IntelliJ IDEA에서 MCP를 사용하려면 최신 업데이트를 적용한다.

 

위에서 빌드한 MCP 서버 실행 파일을 지정한다.

 

Apply 버튼을 클릭하면 위에서 추가한 2개의 기능을 확인 할 수 있다.

 

 

MCP를 사용하기

MCP에 등록된 정보를 사용하여 AI에게 사용방법을 물어본다.

 

사용자 정보를 저장한다.

 

저장된 사용자 목록을 확인한다.

 

 

마무리

짧은 코드로 쉽게 MCP 서버를 개발할 수 있다. 예제에서는 List를 사용하여 사용자 정보를 관리하지만 실제 MCP 서버에서는 DB나 API를 통해서 실제 서비스와 연동할 수 있다. 실제 업무에서는 더욱 다양한 기능을 추가하여 생산성을 올릴 수 있다. 다음 포스팅에는 MCP 보안을 위한 Oauth2 연동 방법에 대하여 작성해 보겠다.

728x90