Files
gitea-mcp/Config/GiteaOptions.cs
T
zhengchen.tao c7fa6aeb7f
Build Docker Image / build (push) Failing after 5m41s
Build Docker Image / deploy (push) Has been skipped
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>
2026-05-06 01:32:42 +08:00

33 lines
1.3 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
namespace GiteaMcp.Config;
/// <summary>
/// Gitea 后端连接配置,通过 env / appsettings 注入。
/// 生产环境敏感字段(AdminPat)必须通过 .env.shared 注入,不要写进代码。
/// </summary>
public class GiteaOptions
{
public const string SectionName = "Gitea";
/// <summary>Gitea 根 URL,例如 https://git.zhengchentao.win(末尾无斜杠)</summary>
public string BaseUrl { get; set; } = "https://git.zhengchentao.win";
/// <summary>
/// Gitea Admin PAT(只读权限:read:repository / read:issue / read:user / read:organization / read:package)。
/// 生产环境从 env Gitea__AdminPat 注入,本地开发用 dotnet user-secrets。
/// 绝对不要 hardcode。
/// </summary>
public string AdminPat { get; set; } = string.Empty;
/// <summary>
/// 黑名单:逗号分隔的 owner/repo,格式如 "zhengchen.tao/secret-repo,org/internal"。
/// 黑名单内的 repo 不会出现在任何 Tool 的返回值里。默认空(全开放)。
/// </summary>
public string RepoBlacklist { get; set; } = string.Empty;
/// <summary>list_repos 的默认 limit(不传时使用)</summary>
public int DefaultLimit { get; set; } = 50;
/// <summary>read_file 的默认最大字节数(1MB</summary>
public int MaxFileBytes { get; set; } = 1 * 1024 * 1024;
}