Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Github server: Allow it to figure out and remember who the Github user is. #575

Open
robertDouglass opened this issue Jan 30, 2025 · 0 comments
Labels
enhancement New feature or request

Comments

@robertDouglass
Copy link
Contributor

Is your feature request related to a problem? Please describe.
If I prompt "Read my blog repo and do a thing", it asks me "What's your Github username?" But, it should know that. It has an access token.

The key issue here is that the MCP server does not currently determine or expose the GitHub username or account associated with the access token (GITHUB_PERSONAL_ACCESS_TOKEN). This causes problems when the LLM tries to execute user-specific queries, such as searching for repositories owned by the authenticated user.

Describe the solution you'd like

Solution: Automatically Determine the GitHub User

To improve accuracy, the MCP server should:

  1. Retrieve the authenticated user's GitHub identity upon startup.
  2. Store this identity for reference in tool executions.
  3. Expose it as an available parameter or auto-fill it in requests when appropriate.

Implementation Plan

  1. Fetch the authenticated GitHub user using the GitHub API's /user endpoint:

    async function getAuthenticatedUser() {
        const response = await fetch("https://api.github.com/user", {
            headers: {
                Authorization: `token ${process.env.GITHUB_PERSONAL_ACCESS_TOKEN}`,
                "User-Agent": "github-mcp-server",
            },
        });
        if (!response.ok) {
            throw new Error(`Failed to fetch authenticated user: ${response.statusText}`);
        }
        return response.json();
    }
  2. Store the username in the server state on initialization:

    let authenticatedGitHubUser = null;
    
    async function initializeServer() {
        try {
            const user = await getAuthenticatedUser();
            authenticatedGitHubUser = user.login;
            console.log(`Authenticated as GitHub user: ${authenticatedGitHubUser}`);
        } catch (error) {
            console.error("Error retrieving GitHub user:", error);
            process.exit(1);
        }
    }
  3. Use this stored username to modify tool behavior:

    • If a request lacks an explicit username, default to authenticatedGitHubUser.
    • Example: Modify repository searches:
      case "search_repositories": {
          const args = repository.SearchRepositoriesSchema.parse(request.params.arguments);
          const username = args.owner || authenticatedGitHubUser; // Use stored username if not provided
          const results = await repository.searchRepositories(username, args.query, args.page, args.perPage);
          return {
              content: [{ type: "text", text: JSON.stringify(results, null, 2) }],
          };
      }
  4. Expose the GitHub username in the ListToolsRequestSchema response so the LLM can reference it explicitly:

    server.setRequestHandler(ListToolsRequestSchema, async () => {
        return {
            tools: [...],
            metadata: {
                authenticatedUser: authenticatedGitHubUser,
            },
        };
    });

Expected Benefits

  • The MCP server will automatically know the GitHub user and use this identity to personalize tool outputs.
  • LLMs interacting with the MCP server will receive the correct user context and be able to search and modify repositories more effectively.
  • Reduces confusion when executing commands that require an explicit user.
@robertDouglass robertDouglass added the enhancement New feature or request label Jan 30, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant