-
Notifications
You must be signed in to change notification settings - Fork 811
feat: optional auth-gated LAN binding for the MCP server #188
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,14 +1,19 @@ | ||
| import Foundation | ||
| import MCP | ||
| import Security | ||
|
|
||
| /// HTTP adapter. Tool handling lives in `ToolExecutor`. | ||
| @Observable | ||
| @MainActor | ||
| final class MCPService { | ||
|
|
||
| static let port: UInt16 = 19789 | ||
| static let loopbackBindHost = MCPHTTPServer.loopbackHost | ||
| static let lanBindHost = "0.0.0.0" | ||
|
|
||
| private static let enabledKey = "io.palmier.pro.mcp.enabled" | ||
| private static let bindHostKey = "io.palmier.pro.mcp.bindHost" | ||
| private static let bearerTokenKey = "io.palmier.pro.mcp.bearerToken" | ||
|
|
||
| static var isEnabledPreference: Bool { | ||
| get { | ||
|
|
@@ -21,6 +26,37 @@ final class MCPService { | |
| } | ||
| } | ||
|
|
||
| static var bindHostPreference: String { | ||
| get { | ||
| MCPHTTPServer.normalizedBindHost(UserDefaults.standard.string(forKey: bindHostKey) ?? loopbackBindHost) | ||
| } | ||
| set { | ||
| UserDefaults.standard.set(MCPHTTPServer.normalizedBindHost(newValue), forKey: bindHostKey) | ||
| } | ||
| } | ||
|
|
||
| static var bearerTokenPreference: String { | ||
| let defaults = UserDefaults.standard | ||
| if let token = defaults.string(forKey: bearerTokenKey), !token.isEmpty { | ||
| return token | ||
| } | ||
| return regenerateBearerTokenPreference() | ||
| } | ||
|
|
||
| static func regenerateBearerTokenPreference() -> String { | ||
| let token = makeBearerToken() | ||
| UserDefaults.standard.set(token, forKey: bearerTokenKey) | ||
| return token | ||
| } | ||
|
|
||
| static var connectionHost: String { | ||
| MCPHTTPServer.advertisedHost(forBindHost: bindHostPreference) | ||
| } | ||
|
|
||
| static var connectionURL: String { | ||
| "http://\(connectionHost):\(port)/mcp" | ||
| } | ||
|
|
||
| private(set) var isRunning: Bool = false | ||
|
|
||
| @ObservationIgnored | ||
|
|
@@ -33,7 +69,9 @@ final class MCPService { | |
| } | ||
|
|
||
| func start() { | ||
| let httpServer = MCPHTTPServer(port: Self.port) { [weak self] in | ||
| let bindHost = Self.bindHostPreference | ||
| let bearerToken = MCPHTTPServer.requiresBearerToken(bindHost: bindHost) ? Self.bearerTokenPreference : "" | ||
| let httpServer = MCPHTTPServer(port: Self.port, bindHost: bindHost, bearerToken: bearerToken) { [weak self] in | ||
| let server = Server( | ||
| name: "palmier-pro", | ||
| version: "1.0.0", | ||
|
|
@@ -51,21 +89,24 @@ final class MCPService { | |
| Task { @MainActor [weak self] in | ||
| do { | ||
| try await httpServer.start() | ||
| Log.mcp.notice("http server started port=\(Self.port)") | ||
| self?.isRunning = true | ||
| guard let self, self.httpServer === httpServer else { return } | ||
| Log.mcp.notice("http server started host=\(bindHost) port=\(Self.port)") | ||
| self.isRunning = true | ||
| } catch { | ||
| guard let self, self.httpServer === httpServer else { return } | ||
| Log.mcp.error("http server failed to start: \(error.localizedDescription)") | ||
| self?.isRunning = false | ||
| self.isRunning = false | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Start task revives stopped listenerHigh Severity
Reviewed by Cursor Bugbot for commit 204caaa. Configure here. |
||
| } | ||
| } | ||
| } | ||
|
|
||
| func stop() { | ||
| if let server = httpServer { | ||
| Task { await server.stop() } | ||
| } | ||
| func stop() async { | ||
| let server = httpServer | ||
| httpServer = nil | ||
| isRunning = false | ||
| if let server { | ||
| await server.stop() | ||
| } | ||
| Log.mcp.notice("http server stopped") | ||
| } | ||
|
|
||
|
|
@@ -132,4 +173,20 @@ final class MCPService { | |
| } | ||
| } | ||
|
|
||
| private static func makeBearerToken() -> String { | ||
| var bytes = [UInt8](repeating: 0, count: 32) | ||
| let count = bytes.count | ||
| let status = bytes.withUnsafeMutableBytes { buffer in | ||
| SecRandomCopyBytes(kSecRandomDefault, count, buffer.baseAddress!) | ||
| } | ||
| guard status == errSecSuccess else { | ||
| return (0..<4).map { _ in UUID().uuidString.replacingOccurrences(of: "-", with: "") }.joined() | ||
| } | ||
| return Data(bytes) | ||
| .base64EncodedString() | ||
| .replacingOccurrences(of: "+", with: "-") | ||
| .replacingOccurrences(of: "/", with: "_") | ||
| .replacingOccurrences(of: "=", with: "") | ||
| } | ||
|
|
||
| } | ||


Uh oh!
There was an error while loading. Please reload this page.