Include meaningful User-Agent header in requests to the GitHub UI

This adds a custom `User-Agent` header to requests from the
GitHub server to the GitHub API, identifying the application,
the version and key information about the environment.

This aligns with the [recommendations][1] in the GitHub Docs.

As part of this change, I have also moved the current version of
the server into a constant, and fix the initialization of `Server`
to use that version, taking from `package.json`.

[1]: https://docs.github.com/en/rest/using-the-rest-api/getting-started-with-the-rest-api?apiVersion=2022-11-28#user-agent
This commit is contained in:
Tim Rogers
2025-02-02 15:01:16 +00:00
parent 0f7730209d
commit df63442c3c
4 changed files with 11 additions and 3 deletions

View File

@@ -1,10 +1,12 @@
import { getUserAgent } from "universal-user-agent";
import { createGitHubError } from "./errors.js";
import { VERSION } from "./version.js";
type RequestOptions = {
method?: string;
body?: unknown;
headers?: Record<string, string>;
};
}
async function parseResponseBody(response: Response): Promise<unknown> {
const contentType = response.headers.get("content-type");
@@ -24,6 +26,8 @@ export function buildUrl(baseUrl: string, params: Record<string, string | number
return url.toString();
}
const USER_AGENT = `modelcontextprotocol/servers/github/v${VERSION} ${getUserAgent()}`;
export async function githubRequest(
url: string,
options: RequestOptions = {}
@@ -31,6 +35,7 @@ export async function githubRequest(
const headers: Record<string, string> = {
"Accept": "application/vnd.github.v3+json",
"Content-Type": "application/json",
"User-Agent": USER_AGENT,
...options.headers,
};