- Notifications
You must be signed in to change notification settings - Fork3k
feat: Add create org invitation tool#1226
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
issei-m wants to merge1 commit intogithub:mainChoose a base branch fromissei-m:org
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
+462 −0
Open
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| { | ||
| "annotations": { | ||
| "title": "Create Organization Invitation", | ||
| "readOnlyHint": false | ||
| }, | ||
| "description": "Invite a user to join an organization by GitHub user ID or email address. Requires organization owner permissions. This endpoint triggers notifications and may be subject to rate limiting.", | ||
| "inputSchema": { | ||
| "properties": { | ||
| "email": { | ||
| "description": "Email address of the person you are inviting. Required unless invitee_id is provided.", | ||
| "type": "string" | ||
| }, | ||
| "invitee_id": { | ||
| "description": "GitHub user ID for the person you are inviting. Required unless email is provided.", | ||
| "type": "number" | ||
| }, | ||
| "org": { | ||
| "description": "The organization name (not case sensitive)", | ||
| "type": "string" | ||
| }, | ||
| "role": { | ||
| "default": "direct_member", | ||
| "description": "The role for the new member", | ||
| "enum": [ | ||
| "admin", | ||
| "direct_member", | ||
| "billing_manager", | ||
| "reinstate" | ||
| ], | ||
| "type": "string" | ||
| }, | ||
| "team_ids": { | ||
| "description": "Team IDs to invite new members to", | ||
| "items": { | ||
| "type": "number" | ||
| }, | ||
| "type": "array" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "org" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "name": "create_org_invitation" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,186 @@ | ||
| package github | ||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "io" | ||
| "net/http" | ||
| "strconv" | ||
| "github.com/google/go-github/v74/github" | ||
| "github.com/mark3labs/mcp-go/mcp" | ||
| "github.com/mark3labs/mcp-go/server" | ||
| "github.com/github/github-mcp-server/pkg/translations" | ||
| ) | ||
| // CreateOrgInvitation creates a new invitation for a user to join an organization | ||
| func CreateOrgInvitation(getClient GetClientFn, t translations.TranslationHelperFunc) (tool mcp.Tool, handler server.ToolHandlerFunc) { | ||
| return mcp.NewTool("create_org_invitation", | ||
| mcp.WithDescription(t("TOOL_CREATE_ORG_INVITATION_DESCRIPTION", "Invite a user to join an organization by GitHub user ID or email address. Requires organization owner permissions. This endpoint triggers notifications and may be subject to rate limiting.")), | ||
| mcp.WithToolAnnotation(mcp.ToolAnnotation{ | ||
| Title: t("TOOL_CREATE_ORG_INVITATION", "Create Organization Invitation"), | ||
| ReadOnlyHint: ToBoolPtr(false), | ||
| }), | ||
| mcp.WithString("org", | ||
| mcp.Required(), | ||
| mcp.Description("The organization name (not case sensitive)"), | ||
| ), | ||
| mcp.WithNumber("invitee_id", | ||
| mcp.Description("GitHub user ID for the person you are inviting. Required unless email is provided."), | ||
| ), | ||
| mcp.WithString("email", | ||
| mcp.Description("Email address of the person you are inviting. Required unless invitee_id is provided."), | ||
| ), | ||
| mcp.WithString("role", | ||
| mcp.Description("The role for the new member"), | ||
| mcp.Enum("admin", "direct_member", "billing_manager", "reinstate"), | ||
| mcp.DefaultString("direct_member"), | ||
| ), | ||
| mcp.WithArray("team_ids", | ||
| mcp.Description("Team IDs to invite new members to"), | ||
| mcp.Items(map[string]any{ | ||
| "type": "number", | ||
| }), | ||
| ), | ||
| ), | ||
| func(ctx context.Context, request mcp.CallToolRequest) (*mcp.CallToolResult, error) { | ||
| org, err := RequiredParam[string](request, "org") | ||
| if err != nil { | ||
| return mcp.NewToolResultError(err.Error()), nil | ||
| } | ||
| inviteeID, err := OptionalParam[float64](request, "invitee_id") | ||
| if err != nil { | ||
| return mcp.NewToolResultError(err.Error()), nil | ||
| } | ||
| email, err := OptionalParam[string](request, "email") | ||
| if err != nil { | ||
| return mcp.NewToolResultError(err.Error()), nil | ||
| } | ||
| // Validate that at least one of invitee_id or email is provided | ||
| if inviteeID == 0 && email == "" { | ||
| return mcp.NewToolResultError("either invitee_id or email must be provided"), nil | ||
| } | ||
| role, err := OptionalParam[string](request, "role") | ||
| if err != nil { | ||
| return mcp.NewToolResultError(err.Error()), nil | ||
| } | ||
| if role == "" { | ||
| role = "direct_member" | ||
| } | ||
| var teamIDs []int64 | ||
| if rawTeamIDs, ok := request.GetArguments()["team_ids"]; ok { | ||
| switch v := rawTeamIDs.(type) { | ||
| case nil: | ||
| // nothing to do | ||
| case []any: | ||
| for _, item := range v { | ||
| id, parseErr := parseTeamID(item) | ||
| if parseErr != nil { | ||
| return mcp.NewToolResultError(parseErr.Error()), nil | ||
| } | ||
| teamIDs = append(teamIDs, id) | ||
| } | ||
| case []float64: | ||
| for _, item := range v { | ||
| teamIDs = append(teamIDs, int64(item)) | ||
| } | ||
| default: | ||
| return mcp.NewToolResultError("team_ids must be an array of numbers"), nil | ||
| } | ||
| } | ||
| client, err := getClient(ctx) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to get GitHub client: %w", err) | ||
| } | ||
| // Create the invitation request | ||
| invitation := &github.CreateOrgInvitationOptions{ | ||
| Role: github.Ptr(role), | ||
| TeamID: teamIDs, | ||
| } | ||
| if inviteeID != 0 { | ||
| invitation.InviteeID = github.Ptr(int64(inviteeID)) | ||
| } | ||
| if email != "" { | ||
| invitation.Email = github.Ptr(email) | ||
| } | ||
| createdInvitation, resp, err := client.Organizations.CreateOrgInvitation(ctx, org, invitation) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to create organization invitation: %w", err) | ||
| } | ||
| defer func() { _ = resp.Body.Close() }() | ||
| if resp.StatusCode != http.StatusCreated { | ||
| body, err := io.ReadAll(resp.Body) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to read response body: %w", err) | ||
| } | ||
| return mcp.NewToolResultError(fmt.Sprintf("failed to create organization invitation: %s", string(body))), nil | ||
| } | ||
| // Return a minimal response with relevant information | ||
| type InvitationResponse struct { | ||
| ID int64 `json:"id"` | ||
| Login string `json:"login,omitempty"` | ||
| Email string `json:"email,omitempty"` | ||
| Role string `json:"role"` | ||
| InvitationTeamsURL string `json:"invitation_teams_url"` | ||
| CreatedAt string `json:"created_at"` | ||
| InviterLogin string `json:"inviter_login,omitempty"` | ||
| } | ||
| response := InvitationResponse{ | ||
| ID: createdInvitation.GetID(), | ||
| Login: createdInvitation.GetLogin(), | ||
| Email: createdInvitation.GetEmail(), | ||
| Role: createdInvitation.GetRole(), | ||
| InvitationTeamsURL: createdInvitation.GetInvitationTeamURL(), | ||
| CreatedAt: createdInvitation.GetCreatedAt().Format("2006-01-02T15:04:05Z07:00"), | ||
| } | ||
| if createdInvitation.Inviter != nil { | ||
| response.InviterLogin = createdInvitation.Inviter.GetLogin() | ||
| } | ||
| r, err := json.Marshal(response) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to marshal response: %w", err) | ||
| } | ||
| return mcp.NewToolResultText(string(r)), nil | ||
| } | ||
| } | ||
| func parseTeamID(value any) (int64, error) { | ||
| switch v := value.(type) { | ||
| case float64: | ||
| // JSON numbers decode to float64; ensure they are whole numbers | ||
| if v != float64(int64(v)) { | ||
| return 0, fmt.Errorf("team_id must be an integer value") | ||
| } | ||
| return int64(v), nil | ||
| case int: | ||
| return int64(v), nil | ||
| case int64: | ||
| return v, nil | ||
| case string: | ||
| id, err := strconv.ParseInt(v, 10, 64) | ||
| if err != nil { | ||
| return 0, fmt.Errorf("invalid team_id") | ||
| } | ||
| return id, nil | ||
| default: | ||
| return 0, fmt.Errorf("invalid team_id") | ||
| } | ||
| } |
Oops, something went wrong.
Uh oh!
There was an error while loading.Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.