intra-mart Accel Platform IM-Copilot MCPサーバ プログラミングガイド 初版 2026-10-01

7. MCPサーバの実装例

ここでは、 MCPサーバの作成方法 で説明した各機能をまとめて実装した例を掲載します。
ドキュメントの検索・取得・要約を行うMCPサーバを、1つのクラスで実装しています。

注意

本書に掲載しているサンプルコードは製品に同梱されていません。
また、サンプルコードは可読性を重視しており、性能面や保守性といった観点において必ずしも適切な実装ではありません。

7.1. MCPサーバクラス

package com.example.mcp;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import jp.co.intra_mart.foundation.copilot.mcp.annotation.Completion;
import jp.co.intra_mart.foundation.copilot.mcp.annotation.MCPServer;
import jp.co.intra_mart.foundation.copilot.mcp.annotation.Prompt;
import jp.co.intra_mart.foundation.copilot.mcp.annotation.PromptArgument;
import jp.co.intra_mart.foundation.copilot.mcp.annotation.Resource;
import jp.co.intra_mart.foundation.copilot.mcp.annotation.Tool;
import jp.co.intra_mart.foundation.copilot.mcp.exception.McpServerException;
import jp.co.intra_mart.foundation.copilot.mcp.model.completion.CompleteArgument;
import jp.co.intra_mart.foundation.copilot.mcp.model.completion.CompleteContext;
import jp.co.intra_mart.foundation.copilot.mcp.model.prompt.PromptMessage;
import jp.co.intra_mart.foundation.copilot.mcp.model.resource.ResourceContents;
import jp.co.intra_mart.foundation.copilot.mcp.model.resource.TextResourceContents;
import jp.co.intra_mart.foundation.copilot.mcp.model.tool.CallToolResult;
import jp.co.intra_mart.foundation.copilot.tool.annotation.SchemaArrayItemProperties.ItemStringProperty;
import jp.co.intra_mart.foundation.copilot.tool.annotation.SchemaProperties.ArrayProperty;
import jp.co.intra_mart.foundation.copilot.tool.annotation.SchemaProperties.IntegerProperty;
import jp.co.intra_mart.foundation.copilot.tool.annotation.SchemaProperties.StringProperty;

@MCPServer(
    name         = "document-server",
    path         = "/documents",
    scope        = "DOCUMENT_MCP",
    title        = "ドキュメント MCP サーバ",
    version      = "1.0.0",
    description  = "ドキュメント管理機能を提供する MCP サーバです",
    instructions = "ドキュメントの検索・取得・要約に使用できます"
)
public class DocumentMcpServer {

    // ---- Prompt ----

    @Prompt(
        name        = "summarize",
        title       = "ドキュメント要約",
        description = "指定カテゴリのドキュメントを要約するプロンプト",
        arguments   = {
            @PromptArgument(name = "category", title = "カテゴリ", required = true),
            @PromptArgument(name = "length", title = "要約の長さ", description = "short / medium / long")
        }
    )
    public List<PromptMessage> summarize(final Map<String, String> args) {

        final String category = args.get("category");
        final String length = args.get("length");
        final String resolvedLength = (length != null) ? length : "medium";

        return Arrays.asList(
                PromptMessage.user(category + " カテゴリのドキュメントを " + resolvedLength + " の長さで要約してください。"));
    }

    // ---- Completion ----

    @Completion(prompt = "summarize")
    public List<String> completeSummarize(final CompleteArgument argument, final CompleteContext context) {

        if (argument == null) {
            return Arrays.asList();
        }

        final String prefix = (argument.value() != null) ? argument.value() : "";

        if ("category".equals(argument.name())) {
            return Arrays.asList("技術仕様", "業務フロー", "FAQ", "リリースノート").stream()
                    .filter(category -> category.startsWith(prefix))
                    .collect(Collectors.toList());
        }
        if ("length".equals(argument.name())) {
            return Arrays.asList("short", "medium", "long").stream()
                    .filter(length -> length.startsWith(prefix))
                    .collect(Collectors.toList());
        }
        return Arrays.asList();
    }

    // ---- Resource(静的リソース) ----

    @Resource(
        name        = "categories",
        uri         = "resource://documents/categories",
        title       = "カテゴリ一覧",
        description = "利用可能なドキュメントカテゴリの一覧",
        mimeType    = "application/json"
    )
    public List<? extends ResourceContents> getCategories() {

        final String json = "[\"技術仕様\",\"業務フロー\",\"FAQ\"]";

        return Arrays.asList(
                new TextResourceContents("resource://documents/categories", "application/json", json));
    }

    // ---- Resource(リソーステンプレート) ----

    @Resource(
        name        = "document-content",
        uri         = "resource://documents/{docId}",
        title       = "ドキュメント本文",
        description = "指定されたIDのドキュメントの本文を返します",
        mimeType    = "text/plain"
    )
    public List<? extends ResourceContents> getDocument(final Map<String, String> vars) throws McpServerException {

        final String docId = vars.get("docId");
        final String content = loadDocument(docId);
        if (content == null) {
            throw new McpServerException("ドキュメントが見つかりません: " + docId);
        }

        return Arrays.asList(
                new TextResourceContents("resource://documents/" + docId, "text/plain", content));
    }

    // ---- Tool ----

    @Tool(
        name        = "search",
        title       = "ドキュメント検索",
        description = "キーワードでドキュメントを全文検索します"
    )
    public List<String> search(final SearchInput input) {
        return performSearch(input.getKeyword(), input.getMaxResults());
    }

    @Tool(
        name         = "search-structured",
        title        = "ドキュメント検索(構造化出力)",
        description  = "キーワードでドキュメントを検索し、構造化データで返します",
        outputSchema = SearchResult.class
    )
    public CallToolResult searchStructured(final SearchInput input) {

        final SearchResult result = performSearchStructured(input.getKeyword(), input.getMaxResults());

        return CallToolResult.builder()
                .addTextContent("検索結果: " + result.getTotalCount() + " 件")
                .structuredContent(result)
                .build();
    }

    // ---- 内部処理 ----

    private String loadDocument(final String docId) {
        return "サンプル本文 (" + docId + ")";
    }

    private List<String> performSearch(final String keyword, final int maxResults) {
        return Arrays.asList("doc-001", "doc-002");
    }

    private SearchResult performSearchStructured(final String keyword, final int maxResults) {
        final List<String> items = performSearch(keyword, maxResults);
        final SearchResult result = new SearchResult();
        result.setItems(items);
        result.setTotalCount(items.size());
        return result;
    }

    // ---- 入力・出力のモデルクラス ----

    public static class SearchInput {

        @StringProperty(description = "検索キーワード", required = true)
        private String keyword;

        @IntegerProperty(description = "最大件数", minimum = 1, maximum = 100)
        private int maxResults = 10;

        public String getKeyword() {
            return this.keyword;
        }

        public void setKeyword(final String keyword) {
            this.keyword = keyword;
        }

        public int getMaxResults() {
            return this.maxResults;
        }

        public void setMaxResults(final int maxResults) {
            this.maxResults = maxResults;
        }

    }

    public static class SearchResult {

        @ArrayProperty(description = "検索結果のドキュメントID一覧", stringItems = @ItemStringProperty)
        private List<String> items;

        @IntegerProperty(description = "総件数")
        private int totalCount;

        public List<String> getItems() {
            return this.items;
        }

        public void setItems(final List<String> items) {
            this.items = items;
        }

        public int getTotalCount() {
            return this.totalCount;
        }

        public void setTotalCount(final int totalCount) {
            this.totalCount = totalCount;
        }

    }

}

7.2. MCPサーバの検出設定

上記のMCPサーバクラスを検出させるため、 McpScanPackageFactory の実装クラスを作成します。
package com.example.mcp;

import java.util.Collection;
import java.util.List;

import jp.co.intra_mart.system.copilot.mcp.annotation.McpScanPackageFactory;

public class DocumentMcpScanPackageFactory implements McpScanPackageFactory {

    @Override
    public Collection<String> getTargetPackages() {
        return List.of("com.example.mcp");
    }

}
作成した実装クラスの完全修飾名を、以下のファイルに記載します。
  • src/main/resources/META-INF/services/jp.co.intra_mart.system.copilot.mcp.annotation.McpScanPackageFactory

    com.example.mcp.DocumentMcpScanPackageFactory
    

    コラム

    詳細は MCPサーバの検出設定 を参照してください。

7.3. 公開される機能

上記の実装により、以下の機能がMCPクライアントへ公開されます。
機能
識別子・URI
説明
プロンプト
summarize
指定カテゴリのドキュメントを要約します。
category、length の2つの引数を持ち、いずれも自動補完に対応します。
リソース
resource://documents/categories
利用可能なカテゴリの一覧を返します。
リソーステンプレート
resource://documents/{docId}
指定されたIDのドキュメントの本文を返します。
ツール
search
キーワードでドキュメントを検索し、ドキュメントIDの一覧を返します。
ツール
search-structured
キーワードでドキュメントを検索し、構造化データで結果を返します。

コラム

動作確認の方法については 作成したMCPサーバの動作確認 を参照してください。