模型上下文协议服务器

提供模型上下文协议(MCP)服务器,用于为模型提供工具和知识,以增强聊天功能。

  • by adamsiedlecki
  • Java
  • 本地部署
  • 更新于
在 GitHub 查看 adamsiedlecki/McpDemo

使用说明

MCP 服务器

跟MCP开玩笑?

MCP(模型上下文协议)服务器用于为模型提供操作的上下文,即工具和知识。

MCP服务器可以连接到聊天,从而改善其性能。

图中显示了与 LLM 模型通信的面板,该模型使用了 MCP 服务器和其中的 vectorStore, 以前下载的PDF。

运行 mcp-test 项目

要启动项目,您需要:

  • 启动ollama服务器并下载LLM模型,例如mxbai-embed-large - 这是读取PDF文件所必需的。
ollama pull mxbai-embed-large
  • 配置 mcp-test 项目的 application.properties
# potrzebne do EmbeddingModel (odczytywanie pdf) - tutaj należy wpisać adres ollamy
spring.ai.ollama.base-url=http://10.0.0.22:11434
  • 构建 mcp-test 项目
mvn clean install

构建后,目标目录应该包含.jar文件。

  • 启动可以使用docker compose进行,并为此做好了准备 项目 config 目录中的示例文件。 .jar 文件应该放在 Dockerfile 旁边的 mcp-test 目录中。 启动通常使用以下命令进行:
docker compose up -d

启动时,日志应该显示以下或类似的信息:

Rozpoczynam ingest PDF gus_jasieniec.pdf
Registered tools: 4
Started McpDemoApplication in 18.891 seconds (process running for 19.218)

活动验证

若要验证 MCP 的功能,您可以执行以下查询:

POST地址:端口/mcp

主体:

{
  "jsonrpc": "2.0",
  "id": 1,
  "method": "initialize",
  "params": {
    "protocolVersion": "2025-03-26",
    "capabilities": {
      "roots": {
        "listChanged": true
      },
      "sampling": {}
    },
    "clientInfo": {
      "name": "ExampleClient",
      "version": "1.0.0"
    }
  }
}

他应该回答这样的东西:

{
    "jsonrpc": "2.0",
    "id": 1,
    "result": {
        "protocolVersion": "2025-03-26",
        "capabilities": {
            "completions": {},
            "prompts": {
                "listChanged": false
            },
            "resources": {
                "subscribe": false,
                "listChanged": false
            },
            "tools": {
                "listChanged": false
            }
        },
        "serverInfo": {
            "name": "mcp-server",
            "version": "1.0.0"
        }
    }
}

POST地址:端口/mcp

{
  "jsonrpc": "2.0",
  "id": 2,
  "method": "tools/list"
}

他应该回答这样的东西:

{
    "jsonrpc": "2.0",
    "id": 2,
    "result": {
        "tools": [
            {
                "name": "employeeLeaveStatus",
                "description": "Sprawdza status urlopu pracownika",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "name": {
                            "type": "string"
                        },
                        "employees": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "usedLeaveDays": {
                                        "type": "integer",
                                        "format": "int32"
                                    },
                                    "yearsOfService": {
                                        "type": "integer",
                                        "format": "int32"
                                    }
                                },
                                "required": [
                                    "name",
                                    "usedLeaveDays",
                                    "yearsOfService"
                                ]
                            }
                        }
                    },
                    "required": [
                        "name",
                        "employees"
                    ],
                    "additionalProperties": false
                }
            },
            {
                "name": "employeesWithoutLeave",
                "description": "Zwraca listę pracowników, którzy nie mają już urlopu",
                "inputSchema": {
                    "type": "object",
                    "properties": {
                        "employees": {
                            "type": "array",
                            "items": {
                                "type": "object",
                                "properties": {
                                    "name": {
                                        "type": "string"
                                    },
                                    "usedLeaveDays": {
                                        "type": "integer",
                                        "format": "int32"
                                    },
                                    "yearsOfService": {
                                        "type": "integer",
                                        "format": "int32"
                                    }
                                },
                                "required": [
                                    "name",
                                    "usedLeaveDays",
                                    "yearsOfService"
                                ]
                            }
                        }
                    },
                    "required": [
                        "employees"
                    ],
                    "additionalProperties": false
                }
            }
        ]
    }
}