-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathMCPOAuthRequestHandler.swift
More file actions
67 lines (58 loc) · 2.57 KB
/
MCPOAuthRequestHandler.swift
File metadata and controls
67 lines (58 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import JSONRPC
import Foundation
import Combine
import Logger
import AppKit
public protocol MCPOAuthRequestHandler {
func handleShowOAuthMessage(
_ request: MCPOAuthRequest,
completion: @escaping (
AnyJSONRPCResponse
) -> Void
)
}
public final class MCPOAuthRequestHandlerImpl: MCPOAuthRequestHandler {
public static let shared = MCPOAuthRequestHandlerImpl()
public func handleShowOAuthMessage(_ request: MCPOAuthRequest, completion: @escaping (AnyJSONRPCResponse) -> Void) {
guard let params = request.params else { return }
Logger.gitHubCopilot.debug("Received MCP OAuth Request: \(params)")
Task { @MainActor in
let confirmResult = showMCPOAuthAlert(params)
let jsonResult = try? JSONEncoder().encode(MCPOAuthResponse(confirm: confirmResult))
let jsonValue = (try? JSONDecoder().decode(JSONValue.self, from: jsonResult ?? Data())) ?? JSONValue.null
completion(AnyJSONRPCResponse(id: request.id, result: jsonValue))
}
}
@MainActor
func showMCPOAuthAlert(_ params: MCPOAuthRequestParams) -> Bool {
let alert = NSAlert()
let mcpConfigString = UserDefaults.shared.value(for: \.gitHubCopilotMCPConfig)
var serverName = params.mcpServer // Default fallback
if let mcpConfigData = mcpConfigString.data(using: .utf8),
let mcpConfig = try? JSONDecoder().decode(JSONValue.self, from: mcpConfigData) {
// Iterate through the servers to find a match for the mcpServer URL
if case .hash(let serversDict) = mcpConfig {
for (userDefinedName, serverConfig) in serversDict {
if let url = serverConfig["url"]?.stringValue {
// Check if the mcpServer URL matches the configured URL
if params.mcpServer.contains(url) || url.contains(params.mcpServer) {
serverName = userDefinedName
break
}
}
}
}
}
alert.messageText = "GitHub Copilot"
alert.informativeText = "The MCP Server Definition '\(serverName)' wants to authenticate to \(params.authLabel)."
alert.alertStyle = .informational
alert.addButton(withTitle: "Continue")
alert.addButton(withTitle: "Cancel")
let response = alert.runModal()
if response == .alertFirstButtonReturn {
return true
} else {
return false
}
}
}