|
| 1 | +// Package servercard provides the GitHub MCP Server's MCP Server Card |
| 2 | +// (SEP-2127) types and a public, no-auth HTTP handler that serves it. |
| 3 | +// |
| 4 | +// A Server Card is a static metadata document that describes a remote MCP |
| 5 | +// server — its identity, repository, and HTTP transport — so clients can |
| 6 | +// discover and connect to it before the protocol handshake. It is remote-only |
| 7 | +// and deliberately does NOT enumerate primitives (tools, resources, prompts) |
| 8 | +// or installable packages; those remain in the MCP Registry document |
| 9 | +// (server.json) and runtime listing. |
| 10 | +// |
| 11 | +// See: |
| 12 | +// - https://github.com/modelcontextprotocol/experimental-ext-server-card |
| 13 | +// - https://github.com/modelcontextprotocol/modelcontextprotocol/pull/2127 |
| 14 | +package servercard |
| 15 | + |
| 16 | +import "net/http" |
| 17 | + |
| 18 | +const ( |
| 19 | + // SchemaURL is the v1 Server Card JSON Schema URI that emitted cards |
| 20 | + // conform to. The schema is versioned by its `vN` path segment. |
| 21 | + SchemaURL = "https://static.modelcontextprotocol.io/schemas/v1/server-card.schema.json" |
| 22 | + |
| 23 | + // MediaType is the media type used to serve and request a Server Card. |
| 24 | + MediaType = "application/mcp-server-card+json" |
| 25 | + |
| 26 | + // Path is the suffix, relative to a server's streamable-HTTP URL, at which |
| 27 | + // MCP reserves the recommended Server Card location. A server hosted at |
| 28 | + // `https://host/mcp` therefore serves its card at `https://host/mcp/server-card`. |
| 29 | + Path = "/server-card" |
| 30 | + |
| 31 | + // DefaultRemoteURL is the streamable-HTTP endpoint of the hosted GitHub MCP |
| 32 | + // Server on github.com. The remote repository overrides this per environment. |
| 33 | + DefaultRemoteURL = "https://api.githubcopilot.com/mcp/" |
| 34 | +) |
| 35 | + |
| 36 | +// Identity fields reused from the MCP Registry document (server.json) so the |
| 37 | +// Server Card and the registry entry describe the same server. |
| 38 | +const ( |
| 39 | + serverName = "io.github.github/github-mcp-server" |
| 40 | + serverTitle = "GitHub" |
| 41 | + serverDescription = "Connect AI assistants to GitHub - manage repos, issues, PRs, and workflows through natural language." |
| 42 | + repositoryURL = "https://github.com/github/github-mcp-server" |
| 43 | + repositorySource = "github" |
| 44 | + // repositoryID is the github.com repository ID for github/github-mcp-server. |
| 45 | + // It is stable across renames but changes if the repository is recreated. |
| 46 | + repositoryID = "942771284" |
| 47 | +) |
| 48 | + |
| 49 | +// ServerCard is a static metadata document describing a remote MCP server, |
| 50 | +// suitable for pre-connection discovery. It mirrors the ServerCard interface in |
| 51 | +// modelcontextprotocol/experimental-ext-server-card. Server Cards are |
| 52 | +// remote-only and never carry installable packages. |
| 53 | +type ServerCard struct { |
| 54 | + // Schema is the Server Card JSON Schema URI this document conforms to. |
| 55 | + Schema string `json:"$schema"` |
| 56 | + // Name is the server name in reverse-DNS format with exactly one slash. |
| 57 | + Name string `json:"name"` |
| 58 | + // Version is the server version, equivalent to Implementation.version. |
| 59 | + Version string `json:"version"` |
| 60 | + // Description is a short, human-readable explanation of server functionality. |
| 61 | + Description string `json:"description"` |
| 62 | + // Title is an optional human-readable display name. |
| 63 | + Title string `json:"title,omitempty"` |
| 64 | + // WebsiteURL optionally links to the server's homepage or documentation. |
| 65 | + WebsiteURL string `json:"websiteUrl,omitempty"` |
| 66 | + // Repository optionally describes the server's source code for inspection. |
| 67 | + Repository *Repository `json:"repository,omitempty"` |
| 68 | + // Remotes lists the HTTP-based endpoints for connecting to the server. |
| 69 | + Remotes []Remote `json:"remotes,omitempty"` |
| 70 | +} |
| 71 | + |
| 72 | +// Repository describes the MCP server's source code location. |
| 73 | +type Repository struct { |
| 74 | + // URL is the repository URL for browsing source and cloning. |
| 75 | + URL string `json:"url"` |
| 76 | + // Source is the hosting service identifier (e.g. "github"). |
| 77 | + Source string `json:"source"` |
| 78 | + // ID is the optional repository identifier owned by the hosting service. |
| 79 | + ID string `json:"id,omitempty"` |
| 80 | +} |
| 81 | + |
| 82 | +// Remote describes a remote (HTTP-based) MCP server endpoint. Authentication is |
| 83 | +// intentionally not described here: the hosted server advertises its auth |
| 84 | +// requirements via OAuth protected-resource-metadata discovery, so duplicating |
| 85 | +// them on the card would risk drift and cannot capture every accepted mode. |
| 86 | +type Remote struct { |
| 87 | + // Type is the transport type ("streamable-http" or "sse"). |
| 88 | + Type string `json:"type"` |
| 89 | + // URL is the endpoint URL. |
| 90 | + URL string `json:"url"` |
| 91 | +} |
| 92 | + |
| 93 | +// Config controls how the GitHub MCP Server card is built and served. |
| 94 | +type Config struct { |
| 95 | + // Version is advertised as the card's version and SHOULD match the |
| 96 | + // runtime serverInfo version. When empty, "0.0.0-dev" is used. |
| 97 | + Version string |
| 98 | + |
| 99 | + // RemoteURL is the absolute streamable-HTTP endpoint advertised in the |
| 100 | + // card's single remote. When empty, DefaultRemoteURL is used. The remote |
| 101 | + // repository supplies a per-environment URL here. |
| 102 | + RemoteURL string |
| 103 | + |
| 104 | + // RemoteURLFunc, when set, derives the streamable-HTTP remote URL from the |
| 105 | + // incoming request, taking precedence over RemoteURL whenever it returns a |
| 106 | + // non-empty value. This supports multi-tenant deployments (e.g. proxima) |
| 107 | + // where the absolute URL varies per request (e.g. from X-Forwarded-Host). |
| 108 | + // |
| 109 | + // It is consumed by the Handler when serving a card; NewServerCard ignores |
| 110 | + // it, since the card constructor is not request-aware. |
| 111 | + RemoteURLFunc func(*http.Request) string |
| 112 | +} |
| 113 | + |
| 114 | +// NewServerCard builds the GitHub MCP Server's Server Card from cfg. |
| 115 | +func NewServerCard(cfg Config) *ServerCard { |
| 116 | + version := cfg.Version |
| 117 | + if version == "" { |
| 118 | + version = "0.0.0-dev" |
| 119 | + } |
| 120 | + |
| 121 | + remoteURL := cfg.RemoteURL |
| 122 | + if remoteURL == "" { |
| 123 | + remoteURL = DefaultRemoteURL |
| 124 | + } |
| 125 | + |
| 126 | + // supportedProtocolVersions is intentionally omitted: the go-sdk does not |
| 127 | + // export the list of versions it negotiates, so we cannot advertise it |
| 128 | + // accurately from the runtime. Omitting it is preferable to publishing a |
| 129 | + // hand-maintained list that could drift from what the server actually |
| 130 | + // serves. |
| 131 | + return &ServerCard{ |
| 132 | + Schema: SchemaURL, |
| 133 | + Name: serverName, |
| 134 | + Version: version, |
| 135 | + Description: serverDescription, |
| 136 | + Title: serverTitle, |
| 137 | + WebsiteURL: repositoryURL, |
| 138 | + Repository: &Repository{ |
| 139 | + URL: repositoryURL, |
| 140 | + Source: repositorySource, |
| 141 | + ID: repositoryID, |
| 142 | + }, |
| 143 | + Remotes: []Remote{ |
| 144 | + {Type: "streamable-http", URL: remoteURL}, |
| 145 | + }, |
| 146 | + } |
| 147 | +} |
0 commit comments