mcp-csharp-publish

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

C# MCP Server Publishing

C# MCP 服务器发布

Publish and deploy MCP servers to their target platforms. stdio servers are distributed as NuGet tool packages. HTTP servers are containerized and deployed to Azure or other container hosts. Both can optionally be listed in the official MCP Registry.
将MCP服务器发布部署到目标平台。stdio类型的服务器以NuGet工具包的形式分发,HTTP类型的服务器会被容器化后部署到Azure或其他容器托管平台,两类服务器都可选择上架到官方MCP Registry方便用户检索。

When to Use

适用场景

  • Packaging a stdio MCP server for NuGet distribution
  • Creating a Docker container for an HTTP MCP server
  • Deploying to Azure Container Apps or App Service
  • Publishing to the official MCP Registry for discoverability
  • Setting up
    server.json
    metadata for the MCP Registry
  • 打包stdio类型的MCP服务器用于NuGet分发
  • 为HTTP类型的MCP服务器创建Docker容器
  • 部署到Azure Container Apps或App Service
  • 发布到官方MCP Registry提升可发现性
  • 为MCP Registry配置
    server.json
    元数据

Stop Signals

不适用场景提示

  • Server not tested yet? → Use
    mcp-csharp-test
    first
  • Server not working locally? → Use
    mcp-csharp-debug
  • No server project yet? → Use
    mcp-csharp-create
  • Publishing a non-MCP NuGet package? → Use
    nuget-trusted-publishing
    instead
  • 服务器还未测试? → 先使用
    mcp-csharp-test
  • 服务器本地运行不正常? → 先使用
    mcp-csharp-debug
  • 还没有服务器项目? → 先使用
    mcp-csharp-create
  • 要发布非MCP的NuGet包? → 改用
    nuget-trusted-publishing

Inputs

输入参数

InputRequiredDescription
Transport typeYes
stdio
→ NuGet path,
http
→ Docker/Azure path
Target destinationYesNuGet.org, Docker registry, Azure Container Apps, Azure App Service, MCP Registry
Project pathYesPath to the
.csproj
file
Package ID / server nameRequired for publishingNuGet
PackageId
or MCP Registry name
参数必填说明
传输类型
stdio
→ 走NuGet发布路径,
http
→ 走Docker/Azure发布路径
目标目的地NuGet.org、Docker镜像仓库、Azure Container Apps、Azure App Service、MCP Registry
项目路径
.csproj
文件的路径
包ID/服务器名称发布时必填NuGet的
PackageId
或MCP Registry中的名称

Workflow

工作流程

Step 1: Choose the publishing path

步骤1:选择发布路径

TransportPrimary DestinationUsers Run With
stdioNuGet.org
dnx YourPackage@version
HTTPDocker → AzureContainer URL
Both paths can optionally publish to the MCP Registry for discoverability.
传输类型主要发布目的地用户使用方式
stdioNuGet.org
dnx YourPackage@version
HTTPDocker → Azure容器URL
两条路径都可选择额外发布到MCP Registry提升可发现性。

Step 2a: NuGet publishing (stdio servers)

步骤2a:NuGet发布(stdio服务器)

  1. Configure
    .csproj
    with package properties:
xml
<PropertyGroup>
  <PackAsTool>true</PackAsTool>
  <ToolCommandName>mymcpserver</ToolCommandName>
  <PackageId>YourUsername.MyMcpServer</PackageId>
  <Version>1.0.0</Version>
  <Authors>Your Name</Authors>
  <Description>MCP server for interacting with MyService</Description>
  <PackageLicenseExpression>MIT</PackageLicenseExpression>
  <PackageTags>mcp;modelcontextprotocol;ai;llm</PackageTags>
  <PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
  <None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
  1. Build and pack:
bash
dotnet build -c Release
dotnet pack -c Release
  1. Test locally before publishing:
bash
dotnet tool install --global --add-source bin/Release/ YourUsername.MyMcpServer
mymcpserver --help          # verify it runs
dotnet tool uninstall --global YourUsername.MyMcpServer
  1. Push to NuGet.org:
bash
dotnet nuget push bin/Release/*.nupkg \
  --api-key YOUR_NUGET_API_KEY \
  --source https://api.nuget.org/v3/index.json
  1. Verify — users configure in
    mcp.json
    :
json
{
  "servers": {
    "MyMcpServer": {
      "type": "stdio",
      "command": "dnx",
      "args": ["YourUsername.MyMcpServer@1.0.0", "--yes"]
    }
  }
}
For detailed NuGet packaging and trusted publishing setup, see references/nuget-packaging.md.
  1. **配置
    .csproj
    **的包属性:
xml
<PropertyGroup>
  <PackAsTool>true</PackAsTool>
  <ToolCommandName>mymcpserver</ToolCommandName>
  <PackageId>YourUsername.MyMcpServer</PackageId>
  <Version>1.0.0</Version>
  <Authors>Your Name</Authors>
  <Description>MCP server for interacting with MyService</Description>
  <PackageLicenseExpression>MIT</PackageLicenseExpression>
  <PackageTags>mcp;modelcontextprotocol;ai;llm</PackageTags>
  <PackageReadmeFile>README.md</PackageReadmeFile>
</PropertyGroup>

<ItemGroup>
  <None Include="README.md" Pack="true" PackagePath="\" />
</ItemGroup>
  1. 构建并打包:
bash
dotnet build -c Release
dotnet pack -c Release
  1. 发布前本地测试:
bash
dotnet tool install --global --add-source bin/Release/ YourUsername.MyMcpServer
mymcpserver --help          # 验证可正常运行
dotnet tool uninstall --global YourUsername.MyMcpServer
  1. 推送到NuGet.org:
bash
dotnet nuget push bin/Release/*.nupkg \
  --api-key YOUR_NUGET_API_KEY \
  --source https://api.nuget.org/v3/index.json
  1. 验证 — 用户在
    mcp.json
    中配置使用:
json
{
  "servers": {
    "MyMcpServer": {
      "type": "stdio",
      "command": "dnx",
      "args": ["YourUsername.MyMcpServer@1.0.0", "--yes"]
    }
  }
}
如需了解详细的NuGet打包和可信发布设置,请查看references/nuget-packaging.md

Step 2b: Docker containerization (HTTP servers)

步骤2b:Docker容器化(HTTP服务器)

  1. Create Dockerfile:
dockerfile
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /app .
  1. 创建Dockerfile:
dockerfile
FROM mcr.microsoft.com/dotnet/sdk:10.0 AS build
WORKDIR /src
COPY *.csproj ./
RUN dotnet restore
COPY . ./
RUN dotnet publish -c Release -o /app

FROM mcr.microsoft.com/dotnet/aspnet:10.0
WORKDIR /app
COPY --from=build /app .

Non-root user for security

出于安全考虑使用非root用户

RUN adduser --disabled-password --gecos '' appuser USER appuser
ENV ASPNETCORE_URLS=http://+:8080 EXPOSE 8080 HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3
CMD curl -f http://localhost:8080/health || exit 1 ENTRYPOINT ["dotnet", "MyMcpServer.dll"]

2. **Build and test locally:**
```bash
docker build -t mymcpserver:latest .
docker run -d -p 3001:8080 -e API_KEY=test-key --name mymcpserver mymcpserver:latest
curl http://localhost:3001/health
  1. Push to container registry:
bash
undefined
RUN adduser --disabled-password --gecos '' appuser USER appuser
ENV ASPNETCORE_URLS=http://+:8080 EXPOSE 8080 HEALTHCHECK --interval=30s --timeout=3s --start-period=10s --retries=3
CMD curl -f http://localhost:8080/health || exit 1 ENTRYPOINT ["dotnet", "MyMcpServer.dll"]

2. **本地构建并测试:**
```bash
docker build -t mymcpserver:latest .
docker run -d -p 3001:8080 -e API_KEY=test-key --name mymcpserver mymcpserver:latest
curl http://localhost:3001/health
  1. 推送到镜像仓库:
bash
undefined

Docker Hub

Docker Hub

docker tag mymcpserver:latest <yourusername>/<mymcpserver>:1.0.0 docker push <yourusername>/<mymcpserver>:1.0.0
docker tag mymcpserver:latest <yourusername>/<mymcpserver>:1.0.0 docker push <yourusername>/<mymcpserver>:1.0.0

Azure Container Registry

Azure Container Registry

az acr login --name yourregistry docker tag mymcpserver:latest <yourregistry>.azurecr.io/<mymcpserver>:1.0.0 docker push <yourregistry>.azurecr.io/<mymcpserver>:1.0.0
undefined
az acr login --name yourregistry docker tag mymcpserver:latest <yourregistry>.azurecr.io/<mymcpserver>:1.0.0 docker push <yourregistry>.azurecr.io/<mymcpserver>:1.0.0
undefined

Step 3: Deploy to Azure (HTTP servers)

步骤3:部署到Azure(HTTP服务器)

Azure Container Apps (recommended — serverless with auto-scaling):
bash
az containerapp create \
  --name mymcpserver \
  --resource-group mygroup \
  --environment myenvironment \
  --image <yourregistry>.azurecr.io/<mymcpserver>:1.0.0 \
  --target-port 8080 \
  --ingress external \
  --min-replicas 0 \
  --max-replicas 10 \
  --secrets api-key=my-actual-api-key \
  --env-vars API_KEY=secretref:api-key
Azure App Service (traditional web hosting):
bash
az webapp create \
  --name mymcpserver \
  --resource-group mygroup \
  --plan myplan \
  --deployment-container-image-name <yourregistry>.azurecr.io/<mymcpserver>:1.0.0
For detailed Azure deployment, see references/docker-azure.md.
Azure Container Apps(推荐 — 支持自动扩缩容的无服务器服务):
bash
az containerapp create \
  --name mymcpserver \
  --resource-group mygroup \
  --environment myenvironment \
  --image <yourregistry>.azurecr.io/<mymcpserver>:1.0.0 \
  --target-port 8080 \
  --ingress external \
  --min-replicas 0 \
  --max-replicas 10 \
  --secrets api-key=my-actual-api-key \
  --env-vars API_KEY=secretref:api-key
Azure App Service(传统Web托管):
bash
az webapp create \
  --name mymcpserver \
  --resource-group mygroup \
  --plan myplan \
  --deployment-container-image-name <yourregistry>.azurecr.io/<mymcpserver>:1.0.0
如需了解详细的Azure部署方法,请查看references/docker-azure.md

Step 4: Publish to MCP Registry (optional)

步骤4:发布到MCP Registry(可选)

List your server in the official MCP Registry for discoverability.
  1. Install
    mcp-publisher
    :
bash
undefined
将你的服务器上架到官方MCP Registry,方便用户检索发现。
  1. 安装
    mcp-publisher
bash
undefined

macOS/Linux

macOS/Linux

brew install mcp-publisher
brew install mcp-publisher

2. **Create `.mcp/server.json`** (or run `mcp-publisher init` to generate interactively):
```json
{
  "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
  "name": "io.github.username/servername",
  "description": "Your server description",
  "version": "1.0.0",
  "packages": [{
    "registryType": "nuget",
    "registryBaseUrl": "https://api.nuget.org",
    "identifier": "YourUsername.MyMcpServer",
    "version": "1.0.0",
    "transport": { "type": "stdio" }
  }],
  "repository": {
    "url": "https://github.com/username/repo",
    "source": "github"
  }
}
Version consistency (critical): The root
version
,
packages[].version
, and
<Version>
in
.csproj
must all match. A mismatch causes registry validation failures or users downloading the wrong version.
  1. Authenticate and publish:
bash
mcp-publisher login github      # name must be io.github.<username>/... for GitHub auth
mcp-publisher publish
  1. Verify:
bash
curl "https://registry.modelcontextprotocol.io/v0.1/servers?search=io.github.<username>/<servername>"
For Registry details (namespace conventions, environment variables, CI/CD automation), see references/mcp-registry.md.

2. **创建`.mcp/server.json`**(也可运行`mcp-publisher init`交互式生成):
```json
{
  "$schema": "https://static.modelcontextprotocol.io/schemas/2025-12-11/server.schema.json",
  "name": "io.github.username/servername",
  "description": "Your server description",
  "version": "1.0.0",
  "packages": [{
    "registryType": "nuget",
    "registryBaseUrl": "https://api.nuget.org",
    "identifier": "YourUsername.MyMcpServer",
    "version": "1.0.0",
    "transport": { "type": "stdio" }
  }],
  "repository": {
    "url": "https://github.com/username/repo",
    "source": "github"
  }
}
版本一致性(非常重要): 根节点的
version
packages[].version
.csproj
中的
<Version>
必须完全一致。版本不匹配会导致Registry校验失败,或者用户下载到错误的版本。
  1. 认证并发布:
bash
mcp-publisher login github      # 如果使用GitHub认证,名称必须符合io.github.<username>/...的格式
mcp-publisher publish
  1. 验证:
bash
curl "https://registry.modelcontextprotocol.io/v0.1/servers?search=io.github.<username>/<servername>"
如需了解Registry的详细信息(命名空间规范、环境变量、CI/CD自动化),请查看references/mcp-registry.md

Step 5: Security checklist

步骤5:安全检查清单

  • No hardcoded secrets — use environment variables or Key Vault
  • HTTPS enabled for HTTP transport in production
  • Health check endpoint implemented
  • Input validation on all tool parameters
  • Rate limiting considered for HTTP servers
  • 无硬编码密钥 — 使用环境变量或Key Vault
  • 生产环境的HTTP传输已启用HTTPS
  • 已实现健康检查端点
  • 所有工具参数都已做输入校验
  • HTTP服务器已考虑限流配置

Validation

校验项

  • NuGet: Package installs and runs via
    dnx PackageId@version
  • Docker: Container starts and health check passes
  • Azure: Server is reachable and tools respond
  • MCP Registry: Server appears at
    registry.modelcontextprotocol.io
  • MCP client can connect and call tools on the deployed server
  • NuGet: 可通过
    dnx PackageId@version
    安装并运行包
  • Docker: 容器可正常启动,健康检查通过
  • Azure: 服务器可正常访问,工具可响应请求
  • MCP Registry: 服务器可在
    registry.modelcontextprotocol.io
    上检索到
  • MCP客户端可连接到部署后的服务器并调用工具

Common Pitfalls

常见问题

PitfallSolution
NuGet package doesn't run as a toolMissing
<PackAsTool>true</PackAsTool>
in
.csproj
Version mismatch between
.csproj
and
server.json
Keep
<Version>
,
server.json
root
version
, and
packages[].version
in sync
Docker container exits immediatelyCheck entrypoint DLL name matches project output. Run
docker logs mymcpserver
for errors
Azure Container App returns 502Target port mismatch. Ensure
--target-port
matches
ASPNETCORE_URLS
port in the container
MCP Registry rejects publishName must follow namespace convention:
io.github.<username>/<name>
for GitHub auth
API keys leaked in Docker imageUse multi-stage builds. Never
COPY
.env
files. Pass secrets via
--env-vars
at runtime
问题解决方案
NuGet包无法作为工具运行
.csproj
中缺少
<PackAsTool>true</PackAsTool>
配置
.csproj
server.json
的版本不匹配
保持
<Version>
server.json
根节点
version
packages[].version
同步
Docker容器立即退出检查入口点DLL名称是否与项目输出匹配,运行
docker logs mymcpserver
查看错误信息
Azure Container App返回502错误目标端口不匹配,确保
--target-port
和容器中
ASPNETCORE_URLS
的端口一致
MCP Registry拒绝发布请求名称必须符合命名空间规范:使用GitHub认证时格式为
io.github.<username>/<name>
API密钥泄露在Docker镜像中使用多阶段构建,永远不要
COPY
.env
文件,运行时通过
--env-vars
传递密钥

Related Skills

相关技能

  • mcp-csharp-create
    — Create a new MCP server project
  • mcp-csharp-debug
    — Running and interactive debugging
  • mcp-csharp-test
    — Automated tests and evaluations
  • mcp-csharp-create
    — 创建新的MCP服务器项目
  • mcp-csharp-debug
    — 运行和交互式调试
  • mcp-csharp-test
    — 自动化测试和评估

Reference Files

参考文件

  • references/nuget-packaging.md — Complete NuGet
    .csproj
    configuration,
    server.json
    for MCP, NuGet.org push, testing with
    dnx
    , version management. Load when: publishing a stdio server to NuGet.
  • references/docker-azure.md — Production Dockerfile patterns, ACR setup, Azure Container Apps full configuration, App Service with Key Vault, secrets management. Load when: deploying an HTTP server to Docker or Azure.
  • references/mcp-registry.md
    mcp-publisher
    CLI installation,
    server.json
    schema, namespace conventions (GitHub vs DNS auth), CI/CD automation. Load when: publishing to the official MCP Registry.
  • references/nuget-packaging.md — 完整的NuGet
    .csproj
    配置、MCP用的
    server.json
    配置、NuGet.org推送、
    dnx
    测试、版本管理。加载时机: 发布stdio服务器到NuGet时。
  • references/docker-azure.md — 生产级Dockerfile模板、ACR设置、Azure Container Apps完整配置、集成Key Vault的App Service、密钥管理。加载时机: 部署HTTP服务器到Docker或Azure时。
  • references/mcp-registry.md
    mcp-publisher
    CLI安装、
    server.json
    schema、命名空间规范(GitHub vs DNS认证)、CI/CD自动化。加载时机: 发布到官方MCP Registry时。

More Info

更多信息