实施说明
MCP服务器设置
服务器使用 metoro-io/mcp-golang 具有HTTP传输层(端口8080)的包,以符合对基于HTTPRESTneneneba API的要求。 资源端点(info://server)已注册以提供有关服务器的元数据,包括其名称、版本和可用操作(create_issue, update_issue, search_issues).这允许MCP客户端发现服务器的功能。
Jira客户端
这 JiraClient struct封装Jira API交互,并确定实例是否为Jira Cloud(基于 .atlassian.net 在URL中)或数据中心。 身份验证的处理方式不同:
- Jira云:使用用户电子邮件和API密钥的基本Auth。
- 数据中心:使用带有API密钥的承载令牌(个人访问令牌)。
这 getBaseAPIPath 方法选择适当的API路径(/rest/api/3 对于云, /rest/api/2 数据中心)来处理端点差异。
支持的操作
- 创建问题:
- 接受 jira_config, project_key, summary, description,以及 issue_type. - 向Jira API的问题端点发送POST请求。 - 返回创建的问题密钥(例如。, PROJ-123).
- 更新问题:
- 接受 jira_config, issue_key, summary,以及 description. - 发送PUT请求以更新指定的问题。 - 返回更新的问题密钥。
- 搜索问题:
- 接受 jira_config 和 jql 查询(例如。, project = PROJ AND status = Open). - 使用JQL查询向搜索端点发送GET请求。 - 返回问题列表及其关键字和摘要。
MCP工具
每个操作都注册为MCP工具,并带有JSON模式注释,以根据MCP规范定义必填和可选字段。 工具使用以下命令以MCP格式返回响应 mcp_golang.NewToolResponse.
安全和配置
这 JiraConfig 结构包含Jira URL、API密钥和电子邮件(用于云身份验证)。 对于这个例子,配置是在工具处理程序中硬编码的。在生产环境中,为了安全起见,您应该从环境变量或配置文件加载这些变量。
错误处理
该代码包括对HTTP请求、JSON封送处理/取消封送处理和API响应的全面错误处理。 不成功的HTTP状态代码会导致描述性错误消息。
如何跑步
再进行
bashgo get github.com/metoro-io/mcp-golang
运行服务器: bash
go run main.go\ 服务器将于启动 http://localhost:8080.
使用MCP客户端进行测试
配置您的MCP客户端以发送请求 jira_config 字段,如示例有效载荷所示。 验证服务器是否正确处理每个请求的Jira URL、电子邮件和API密钥。
其他注意事项
安全
- 在每个请求中传输API密钥需要安全通信(例如HTTPS)。确保服务器配置为在生产环境中使用TLS。
- 考虑验证提供的Jira URL(例如,检查有效域)以防止误用。
错误处理
中现有的错误处理 JiraClient 将捕获无效URL或凭据等问题。您可能希望为添加特定的验证 JiraConfig 字段(例如,确保URL格式正确或电子邮件对云实例有效)。
演出
创建新 JiraClient 因为每个请求都很简单,但可能会引入开销。如果性能成为问题,请考虑缓存常用配置的客户端(例如,使用由URL和电子邮件键入的映射)。
Jira数据中心
对于数据中心,可能不需要电子邮件字段,因为身份验证通常仅使用API令牌。您可以在 JiraConfig struct并跳过数据中心实例的Basic Auth,但由于 JiraClient 适当地处理身份验证。
测试
要测试更新的服务器,请执行以下操作:
Use a tool like curl or an MCP client to send requests with the `jira_config` fields.
Verify that the server correctly processes different Jira URLs (Cloud and Data Center) and authentication credentials.
Check that the actions (create_issue, update_issue, search_issues) return the expected results.例如,为了测试 create_issue 使用curl(假设MCP服务器支持原始HTTP POST进行测试):
bash
-H "Content-Type: application/json" \
-d '{
"jira_config": {
"url": "https://your-jira-instance.atlassian.net",
"api_key": "your-api-key",
"email": "your.email@example.com"
},
"project_key": "PROJ",
"summary": "Test Issue",
"description": "This is a test",
"issue_type": "Task"
}'```
### Client examples
### Example Client Requests
Below are example JSON payloads for each action:
**Create Issue**
{ "function": "create_issue", "parameters": { "jira_config": { "url": "https://your-jira-instance.atlassian.net", "api_key": "your-api-key", "email": "your.email@example.com" }, "project_key": "PROJ", "summary": "Test Issue", "description": "This is a test issue created via MCP", "issue_type": "Task" } }
**更新问题**
{ "function": "update_issue", "parameters": { "jira_config": { "url": "https://your-jira-instance.atlassian.net", "api_key": "your-api-key", "email": "your.email@example.com" }, "issue_key": "PROJ-123", "summary": "Updated Test Issue", "description": "Updated description" } }
**搜索问题**
{ "function": "search_issues", "parameters": { "jira_config": { "url": "https://your-jira-instance.atlassian.net", "api_key": "your-api-key", "email": "your.email@example.com" }, "jql": "project = PROJ AND status = Open" } }
