Skip to content

Implement MCP initialization handshake and capability discovery - #313

Merged
malibio merged 2 commits into
mainfrom
feature/issue-308-mcp-initialization-handshake
Oct 21, 2025
Merged

malibio merged 2 commits into
mainfrom
feature/issue-308-mcp-initialization-handshake

Conversation

@malibio

@malibio malibio commented Oct 21, 2025

Copy link
Copy Markdown
Collaborator

Closes #308

malibio and others added 2 commits October 21, 2025 09:24
…closes #308)

This commit adds the MCP initialization handshake to enable proper capability
negotiation between NodeSpace and MCP clients like Claude Code.

## Changes

### New Files
- `packages/core/src/mcp/handlers/initialize.rs`: Initialize handler with protocol version validation and tool schema generation
- `packages/core/src/mcp/handlers/initialize_test.rs`: Comprehensive unit tests for initialize handler (11 test cases)

### Modified Files
- `packages/core/src/mcp/types.rs`: Added MCPNotification struct for notifications (no id field)
- `packages/core/src/mcp/types_test.rs`: Added 7 notification parsing tests
- `packages/core/src/mcp/server.rs`:
  - Added ServerState with AtomicBool for initialization tracking
  - Updated message loop to handle both requests and notifications
  - Added initialize and ping method handlers
  - Added state validation (blocks operations before initialization)
  - Added handle_notification function for initialized notification
- `packages/core/src/mcp/handlers/mod.rs`: Exported initialize module

## Features

### Protocol Compliance
- ✅ Version negotiation (accepts 2024-11-05, rejects others)
- ✅ Capability advertisement with full tool schemas
- ✅ Notification support for `initialized` event
- ✅ State tracking prevents operations before initialization
- ✅ Ping method for health checks

### Tool Schemas
Manual schemas for all 6 MCP methods:
- create_node, get_node, update_node, delete_node, query_nodes, create_nodes_from_markdown

Each schema includes:
- Method name and description
- Full JSON Schema for parameters
- Required field specifications
- Enum constraints for node types

### Testing
- 11 initialize handler unit tests
- 7 notification parsing tests
- All 55 MCP tests passing
- Quality checks passing (cargo clippy, fmt)

## MCP Lifecycle

1. Client sends `initialize` request with protocol version
2. Server validates version and returns capabilities
3. Client sends `initialized` notification
4. Server marks session as initialized
5. Normal operations begin

Operations attempted before initialization receive error:
"Session not initialized. Send initialize request first."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
This commit addresses the minor suggestions from the pragmatic code review:

1. **Integration Tests**: Added 4 comprehensive integration tests for full MCP handshake flow
   - test_full_initialization_handshake: Complete flow from uninitialized → initialized → operations allowed
   - test_operation_rejected_before_initialization: Verifies all operations blocked before init
   - test_initialize_then_wrong_version: Version validation works even after initialization
   - test_initialize_idempotent: Initialize can be called multiple times safely

2. **Enhanced Error Diagnostics**: Improved logging for message parsing failures
   - Now provides detailed diagnostics when both request and notification parsing fail
   - Distinguishes between: empty messages, invalid JSON, missing fields, wrong version, malformed structures
   - Helps debugging by showing specific validation failures instead of generic "parse error"

Changes:
- packages/core/src/mcp/handlers/initialize_test.rs: +195 lines (4 new integration tests)
- packages/core/src/mcp/server.rs: Enhanced parse error handling with detailed diagnostics

All 59 MCP tests passing. No lint/clippy warnings.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>
@malibio
malibio merged commit 5750d7b into main Oct 21, 2025
@malibio
malibio deleted the feature/issue-308-mcp-initialization-handshake branch October 21, 2025 08:14
malibio added a commit that referenced this pull request Feb 4, 2026
* feat: Implement MCP initialization handshake and capability discovery (closes #308)

This commit adds the MCP initialization handshake to enable proper capability
negotiation between NodeSpace and MCP clients like Claude Code.

## Changes

### New Files
- `packages/core/src/mcp/handlers/initialize.rs`: Initialize handler with protocol version validation and tool schema generation
- `packages/core/src/mcp/handlers/initialize_test.rs`: Comprehensive unit tests for initialize handler (11 test cases)

### Modified Files
- `packages/core/src/mcp/types.rs`: Added MCPNotification struct for notifications (no id field)
- `packages/core/src/mcp/types_test.rs`: Added 7 notification parsing tests
- `packages/core/src/mcp/server.rs`:
  - Added ServerState with AtomicBool for initialization tracking
  - Updated message loop to handle both requests and notifications
  - Added initialize and ping method handlers
  - Added state validation (blocks operations before initialization)
  - Added handle_notification function for initialized notification
- `packages/core/src/mcp/handlers/mod.rs`: Exported initialize module

## Features

### Protocol Compliance
- ✅ Version negotiation (accepts 2024-11-05, rejects others)
- ✅ Capability advertisement with full tool schemas
- ✅ Notification support for `initialized` event
- ✅ State tracking prevents operations before initialization
- ✅ Ping method for health checks

### Tool Schemas
Manual schemas for all 6 MCP methods:
- create_node, get_node, update_node, delete_node, query_nodes, create_nodes_from_markdown

Each schema includes:
- Method name and description
- Full JSON Schema for parameters
- Required field specifications
- Enum constraints for node types

### Testing
- 11 initialize handler unit tests
- 7 notification parsing tests
- All 55 MCP tests passing
- Quality checks passing (cargo clippy, fmt)

## MCP Lifecycle

1. Client sends `initialize` request with protocol version
2. Server validates version and returns capabilities
3. Client sends `initialized` notification
4. Server marks session as initialized
5. Normal operations begin

Operations attempted before initialization receive error:
"Session not initialized. Send initialize request first."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* refactor: Address code review nits for MCP initialization

This commit addresses the minor suggestions from the pragmatic code review:

1. **Integration Tests**: Added 4 comprehensive integration tests for full MCP handshake flow
   - test_full_initialization_handshake: Complete flow from uninitialized → initialized → operations allowed
   - test_operation_rejected_before_initialization: Verifies all operations blocked before init
   - test_initialize_then_wrong_version: Version validation works even after initialization
   - test_initialize_idempotent: Initialize can be called multiple times safely

2. **Enhanced Error Diagnostics**: Improved logging for message parsing failures
   - Now provides detailed diagnostics when both request and notification parsing fail
   - Distinguishes between: empty messages, invalid JSON, missing fields, wrong version, malformed structures
   - Helps debugging by showing specific validation failures instead of generic "parse error"

Changes:
- packages/core/src/mcp/handlers/initialize_test.rs: +195 lines (4 new integration tests)
- packages/core/src/mcp/server.rs: Enhanced parse error handling with detailed diagnostics

All 59 MCP tests passing. No lint/clippy warnings.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 5, 2026
* feat: Implement MCP initialization handshake and capability discovery (closes #308)

This commit adds the MCP initialization handshake to enable proper capability
negotiation between NodeSpace and MCP clients like Claude Code.

## Changes

### New Files
- `packages/core/src/mcp/handlers/initialize.rs`: Initialize handler with protocol version validation and tool schema generation
- `packages/core/src/mcp/handlers/initialize_test.rs`: Comprehensive unit tests for initialize handler (11 test cases)

### Modified Files
- `packages/core/src/mcp/types.rs`: Added MCPNotification struct for notifications (no id field)
- `packages/core/src/mcp/types_test.rs`: Added 7 notification parsing tests
- `packages/core/src/mcp/server.rs`:
  - Added ServerState with AtomicBool for initialization tracking
  - Updated message loop to handle both requests and notifications
  - Added initialize and ping method handlers
  - Added state validation (blocks operations before initialization)
  - Added handle_notification function for initialized notification
- `packages/core/src/mcp/handlers/mod.rs`: Exported initialize module

## Features

### Protocol Compliance
- ✅ Version negotiation (accepts 2024-11-05, rejects others)
- ✅ Capability advertisement with full tool schemas
- ✅ Notification support for `initialized` event
- ✅ State tracking prevents operations before initialization
- ✅ Ping method for health checks

### Tool Schemas
Manual schemas for all 6 MCP methods:
- create_node, get_node, update_node, delete_node, query_nodes, create_nodes_from_markdown

Each schema includes:
- Method name and description
- Full JSON Schema for parameters
- Required field specifications
- Enum constraints for node types

### Testing
- 11 initialize handler unit tests
- 7 notification parsing tests
- All 55 MCP tests passing
- Quality checks passing (cargo clippy, fmt)

## MCP Lifecycle

1. Client sends `initialize` request with protocol version
2. Server validates version and returns capabilities
3. Client sends `initialized` notification
4. Server marks session as initialized
5. Normal operations begin

Operations attempted before initialization receive error:
"Session not initialized. Send initialize request first."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* refactor: Address code review nits for MCP initialization

This commit addresses the minor suggestions from the pragmatic code review:

1. **Integration Tests**: Added 4 comprehensive integration tests for full MCP handshake flow
   - test_full_initialization_handshake: Complete flow from uninitialized → initialized → operations allowed
   - test_operation_rejected_before_initialization: Verifies all operations blocked before init
   - test_initialize_then_wrong_version: Version validation works even after initialization
   - test_initialize_idempotent: Initialize can be called multiple times safely

2. **Enhanced Error Diagnostics**: Improved logging for message parsing failures
   - Now provides detailed diagnostics when both request and notification parsing fail
   - Distinguishes between: empty messages, invalid JSON, missing fields, wrong version, malformed structures
   - Helps debugging by showing specific validation failures instead of generic "parse error"

Changes:
- packages/core/src/mcp/handlers/initialize_test.rs: +195 lines (4 new integration tests)
- packages/core/src/mcp/server.rs: Enhanced parse error handling with detailed diagnostics

All 59 MCP tests passing. No lint/clippy warnings.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
malibio added a commit that referenced this pull request Feb 26, 2026
* feat: Implement MCP initialization handshake and capability discovery (closes #308)

This commit adds the MCP initialization handshake to enable proper capability
negotiation between NodeSpace and MCP clients like Claude Code.

## Changes

### New Files
- `packages/core/src/mcp/handlers/initialize.rs`: Initialize handler with protocol version validation and tool schema generation
- `packages/core/src/mcp/handlers/initialize_test.rs`: Comprehensive unit tests for initialize handler (11 test cases)

### Modified Files
- `packages/core/src/mcp/types.rs`: Added MCPNotification struct for notifications (no id field)
- `packages/core/src/mcp/types_test.rs`: Added 7 notification parsing tests
- `packages/core/src/mcp/server.rs`:
  - Added ServerState with AtomicBool for initialization tracking
  - Updated message loop to handle both requests and notifications
  - Added initialize and ping method handlers
  - Added state validation (blocks operations before initialization)
  - Added handle_notification function for initialized notification
- `packages/core/src/mcp/handlers/mod.rs`: Exported initialize module

## Features

### Protocol Compliance
- ✅ Version negotiation (accepts 2024-11-05, rejects others)
- ✅ Capability advertisement with full tool schemas
- ✅ Notification support for `initialized` event
- ✅ State tracking prevents operations before initialization
- ✅ Ping method for health checks

### Tool Schemas
Manual schemas for all 6 MCP methods:
- create_node, get_node, update_node, delete_node, query_nodes, create_nodes_from_markdown

Each schema includes:
- Method name and description
- Full JSON Schema for parameters
- Required field specifications
- Enum constraints for node types

### Testing
- 11 initialize handler unit tests
- 7 notification parsing tests
- All 55 MCP tests passing
- Quality checks passing (cargo clippy, fmt)

## MCP Lifecycle

1. Client sends `initialize` request with protocol version
2. Server validates version and returns capabilities
3. Client sends `initialized` notification
4. Server marks session as initialized
5. Normal operations begin

Operations attempted before initialization receive error:
"Session not initialized. Send initialize request first."

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

* refactor: Address code review nits for MCP initialization

This commit addresses the minor suggestions from the pragmatic code review:

1. **Integration Tests**: Added 4 comprehensive integration tests for full MCP handshake flow
   - test_full_initialization_handshake: Complete flow from uninitialized → initialized → operations allowed
   - test_operation_rejected_before_initialization: Verifies all operations blocked before init
   - test_initialize_then_wrong_version: Version validation works even after initialization
   - test_initialize_idempotent: Initialize can be called multiple times safely

2. **Enhanced Error Diagnostics**: Improved logging for message parsing failures
   - Now provides detailed diagnostics when both request and notification parsing fail
   - Distinguishes between: empty messages, invalid JSON, missing fields, wrong version, malformed structures
   - Helps debugging by showing specific validation failures instead of generic "parse error"

Changes:
- packages/core/src/mcp/handlers/initialize_test.rs: +195 lines (4 new integration tests)
- packages/core/src/mcp/server.rs: Enhanced parse error handling with detailed diagnostics

All 59 MCP tests passing. No lint/clippy warnings.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <[email protected]>

---------

Co-authored-by: Claude <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement MCP initialization handshake and capability discovery

1 participant