Add configurable strict mode for MCP server initialization (#3)

Adds a Configuration struct to control server behavior around client initialization.
When strict mode is enabled, server enforces MCP spec requiring clients to initialize
before sending requests. Default mode is more permissive for non-compliant clients.
This commit is contained in:
Mattt 2025-03-04 06:02:43 -08:00 • committed by GitHub
commit 1f5f8d64b9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -8,6 +8,25 @@ import class Foundation.JSONEncoder
/// Model Context Protocol server /// Model Context Protocol server
public actor Server { public actor Server {
/// The server configuration
public struct Configuration: Hashable, Codable, Sendable {
/// The default configuration.
public static let `default` = Configuration(strict: false)
/// The strict configuration.
public static let strict = Configuration(strict: true)
/// When strict mode is enabled, the server:
/// - Requires clients to send an initialize request before any other requests
/// - Rejects all requests from uninitialized clients with a protocol error
///
/// While the MCP specification requires clients to initialize the connection
/// before sending other requests, some implementations may not follow this.
/// Disabling strict mode allows the server to be more lenient with non-compliant
/// clients, though this may lead to undefined behavior.
public var strict: Bool
}
/// Implementation information /// Implementation information
public struct Info: Hashable, Codable, Sendable { public struct Info: Hashable, Codable, Sendable {
/// The server name /// The server name
@ -110,7 +129,9 @@ public actor Server {
/// The server version /// The server version
public nonisolated var version: String { serverInfo.version } public nonisolated var version: String { serverInfo.version }
/// The server capabilities /// The server capabilities
public var capabilities = Capabilities() public var capabilities: Capabilities
/// The server configuration
public var configuration: Configuration
/// Request handlers /// Request handlers
private var methodHandlers: [String: RequestHandlerBox] = [:] private var methodHandlers: [String: RequestHandlerBox] = [:]
@ -133,10 +154,12 @@ public actor Server {
public init( public init(
name: String, name: String,
version: String, version: String,
capabilities: Server.Capabilities = .init() capabilities: Server.Capabilities = .init(),
configuration: Configuration = .default
) { ) {
self.serverInfo = Server.Info(name: name, version: version) self.serverInfo = Server.Info(name: name, version: version)
self.capabilities = capabilities self.capabilities = capabilities
self.configuration = configuration
} }
/// Start the server /// Start the server
@ -274,13 +297,15 @@ public actor Server {
"id": "\(request.id)", "id": "\(request.id)",
]) ])
// The client SHOULD NOT send requests other than pings if configuration.strict {
// before the server has responded to the initialize request. // The client SHOULD NOT send requests other than pings
switch request.method { // before the server has responded to the initialize request.
case Initialize.name, Ping.name: switch request.method {
break case Initialize.name, Ping.name:
default: break
try checkInitialized() default:
try checkInitialized()
}
} }
// Find handler for method name // Find handler for method name
@ -308,9 +333,11 @@ public actor Server {
"Processing notification", "Processing notification",
metadata: ["method": "\(message.method)"]) metadata: ["method": "\(message.method)"])
// Check initialization state unless this is an initialized notification if configuration.strict {
if message.method != InitializedNotification.name { // Check initialization state unless this is an initialized notification
try checkInitialized() if message.method != InitializedNotification.name {
try checkInitialized()
}
} }
// Find notification handlers for this method // Find notification handlers for this method