gitea-mcp: 初次落地 Gitea MCP Server (.NET 10, V1 only-read)
把 Gitea (git.zhengchentao.win) 通过 MCP 暴露给 Claude.ai:列 repo、读代码、看 commits / issues / PR / orgs / packages / actions。 设计文档见 vault Coding/gitea-mcp/gitea-mcp 设计.md。 代码模板复用 obsidian-mcp(.NET 10 + ModelContextProtocol SDK + JwtBearer)。 19 个只读 Tool(全部 scope=read:gitea): Repo / 文件: - list_repos / read_repo - list_tree(max_entries=500 防爆) - read_file(max_bytes=1MB,超出 truncated=true) - search_code(走 /repos/search-code,indexer 未启用时返回结构化错误说明) 分支 / 提交: - list_branches / list_commits / read_commit(diff 文件数限 50) Issue / PR: - list_issues / read_issue(含评论) - list_pulls / read_pull(含评论 + 改动文件列表) Org / Package(用户额外授权 read:organization + read:package): - list_orgs / read_org - list_packages / read_package Gitea Actions(运维友好): - list_workflow_runs / read_run_log 技术栈: - .NET 10 + ModelContextProtocol SDK 1.0 - HttpClientFactory + Microsoft.Extensions.Http.Resilience(指数 backoff,5xx/429/网络错误重试) - JwtBearer (HS256, Current+Previous fallback, MapInboundClaims=false) - aud=gitea, scope=read:gitea, iss=https://auth.zhengchentao.win Gitea API client: - Authorization: token <PAT> (admin PAT,仅 read scope) - BaseUrl=https://git.zhengchentao.win - 错误映射:401/403 → UnauthorizedAccessException,404 → KeyNotFoundException,5xx → InvalidOperationException - RepoBlacklist 黑名单(owner/repo 精确匹配,默认空) 部署: - Dockerfile multi-stage,COPY --chown,non-root user - .gitea/workflows/build-image.yml:build + deploy 双 job,buildkit v0.13.2 - 容器内 :8080,宿主端口 9092 - 子域名 git-mcp.zhengchentao.win(区别于 Gitea 本体 git.zhengchentao.win) 测试:6/6 单测过(GiteaRepoFilter 黑名单匹配) Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,89 @@
|
||||
using GiteaMcp.Services;
|
||||
using ModelContextProtocol.Server;
|
||||
using System.ComponentModel;
|
||||
|
||||
namespace GiteaMcp.Tools;
|
||||
|
||||
/// <summary>Issue Tool:list_issues / read_issue</summary>
|
||||
[McpServerToolType]
|
||||
public class IssueTools(
|
||||
GiteaApiClient gitea,
|
||||
GiteaRepoFilter filter)
|
||||
{
|
||||
[McpServerTool]
|
||||
[Description(
|
||||
"List issues in a Gitea repository. " +
|
||||
"state can be 'open' (default), 'closed', or 'all'. " +
|
||||
"Returns: issue number, title, state, labels, assignees, created_at, comment count, and URL. " +
|
||||
"Use read_issue to get the full body and comments of a specific issue.")]
|
||||
public async Task<object> list_issues(
|
||||
[Description("Repository owner.")] string owner,
|
||||
[Description("Repository name.")] string repo,
|
||||
[Description("Filter by state: 'open', 'closed', or 'all'. Default 'open'.")] string? state = null,
|
||||
[Description("Max issues to return. Default 30.")] int? limit = null,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
if (filter.IsBlocked($"{owner}/{repo}"))
|
||||
throw new UnauthorizedAccessException($"Repo {owner}/{repo} is on the access blocklist.");
|
||||
|
||||
var st = state ?? "open";
|
||||
var lim = Math.Min(limit ?? 30, 50);
|
||||
var issues = await gitea.GetIssuesAsync(owner, repo, st, lim, ct);
|
||||
|
||||
return issues.Select(i => new
|
||||
{
|
||||
number = i.Number,
|
||||
title = i.Title,
|
||||
state = i.State,
|
||||
html_url = i.HtmlUrl,
|
||||
author = i.User?.Login,
|
||||
labels = i.Labels?.Select(l => l.Name).ToList() ?? [],
|
||||
assignees = i.Assignees?.Select(a => a.Login).ToList() ?? [],
|
||||
comments = i.Comments,
|
||||
created_at = i.CreatedAt,
|
||||
updated_at = i.UpdatedAt,
|
||||
closed_at = i.ClosedAt,
|
||||
}).ToList();
|
||||
}
|
||||
|
||||
[McpServerTool]
|
||||
[Description(
|
||||
"Get the full body and all comments of a specific Gitea issue. " +
|
||||
"Use list_issues first to find the issue number. " +
|
||||
"Returns: title, body (Markdown), state, labels, and all comment texts with authors and timestamps.")]
|
||||
public async Task<object> read_issue(
|
||||
[Description("Repository owner.")] string owner,
|
||||
[Description("Repository name.")] string repo,
|
||||
[Description("Issue number (integer, e.g. 42).")] int number,
|
||||
CancellationToken ct = default)
|
||||
{
|
||||
if (filter.IsBlocked($"{owner}/{repo}"))
|
||||
throw new UnauthorizedAccessException($"Repo {owner}/{repo} is on the access blocklist.");
|
||||
|
||||
var issue = await gitea.GetIssueAsync(owner, repo, number, ct);
|
||||
var comments = await gitea.GetIssueCommentsAsync(owner, repo, number, ct);
|
||||
|
||||
return new
|
||||
{
|
||||
number = issue.Number,
|
||||
title = issue.Title,
|
||||
body = issue.Body,
|
||||
state = issue.State,
|
||||
html_url = issue.HtmlUrl,
|
||||
author = issue.User?.Login,
|
||||
labels = issue.Labels?.Select(l => l.Name).ToList() ?? [],
|
||||
assignees = issue.Assignees?.Select(a => a.Login).ToList() ?? [],
|
||||
created_at = issue.CreatedAt,
|
||||
updated_at = issue.UpdatedAt,
|
||||
closed_at = issue.ClosedAt,
|
||||
comments = comments.Select(c => new
|
||||
{
|
||||
id = c.Id,
|
||||
author = c.User?.Login,
|
||||
body = c.Body,
|
||||
created_at = c.CreatedAt,
|
||||
updated_at = c.UpdatedAt,
|
||||
}).ToList(),
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user