Skip to content

Use IMemoryCache for process-local tool embeddings#3

Merged
KSemenenko merged 2 commits intocodex/mcp-agent-framework-chat-integrationfrom
codex/imemorycache-embedding-store
Mar 8, 2026
Merged

Use IMemoryCache for process-local tool embeddings#3
KSemenenko merged 2 commits intocodex/mcp-agent-framework-chat-integrationfrom
codex/imemorycache-embedding-store

Conversation

@KSemenenko
Copy link
Member

Summary

  • replace the built-in process-local embedding store internals with IMemoryCache
  • add AddMcpGatewayInMemoryToolEmbeddingStore() and update README, architecture docs, and ADRs
  • remove obsolete/legacy leftovers and update tests around cache-backed behavior

Verification

  • dotnet restore ManagedCode.MCPGateway.slnx
  • dotnet build ManagedCode.MCPGateway.slnx -c Release --no-restore
  • dotnet build ManagedCode.MCPGateway.slnx -c Release --no-restore -p:RunAnalyzers=true
  • dotnet test --solution ManagedCode.MCPGateway.slnx -c Release --no-build
  • roslynator analyze src/ManagedCode.MCPGateway/ManagedCode.MCPGateway.csproj -p Configuration=Release --severity-level warning
  • roslynator analyze tests/ManagedCode.MCPGateway.Tests/ManagedCode.MCPGateway.Tests.csproj -p Configuration=Release --severity-level warning
  • cloc --include-lang=C# src tests

Copilot AI review requested due to automatic review settings March 8, 2026 09:08
Copy link

@chatgpt-codex-connector chatgpt-codex-connector bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 328f7305b7

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the gateway’s process-local tool embedding reuse to be backed by IMemoryCache, adds a dedicated DI registration helper for that store, and cleans up legacy/obsolete paths and documentation to match the new design.

Changes:

  • Reimplemented McpGatewayInMemoryToolEmbeddingStore on top of IMemoryCache (including deterministic fingerprint-agnostic fallback behavior) and removed the old internal index helper.
  • Added AddMcpGatewayInMemoryToolEmbeddingStore() for DI registration and updated tests/docs/ADRs accordingly.
  • Renamed “legacy” lexical naming to “heuristic” in runtime search internals and refreshed related test data.

Reviewed changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
tests/ManagedCode.MCPGateway.Tests/TestSupport/TestToolEmbeddingStore.cs Updates test embedding store to track embeddings directly after internal index removal.
tests/ManagedCode.MCPGateway.Tests/Search/McpGatewaySearchLexicalTests.cs Renames a test context key from legacy to compatibility.
tests/ManagedCode.MCPGateway.Tests/Search/McpGatewayInitializationTests.cs Adds DI test coverage for AddMcpGatewayInMemoryToolEmbeddingStore() and shared IMemoryCache.
tests/ManagedCode.MCPGateway.Tests/Search/McpGatewayInMemoryToolEmbeddingStoreTests.cs Updates disposal pattern and adds test for “latest embedding wins” fallback behavior.
src/ManagedCode.MCPGateway/Registration/McpGatewayServiceCollectionExtensions.cs Adds AddMcpGatewayInMemoryToolEmbeddingStore() (registers IMemoryCache + store).
src/ManagedCode.MCPGateway/Registration/McpGatewayChatOptionsExtensions.cs Removes obsolete AddManagedCodeMcpGatewayTools(...) shims.
src/ManagedCode.MCPGateway/ManagedCode.MCPGateway.csproj Adds package reference for Microsoft.Extensions.Caching.Memory.
src/ManagedCode.MCPGateway/Internal/Runtime/Search/McpGatewayRuntime.TokenSearch.cs Renames legacy lexical scoring concepts to heuristic lexical scoring.
src/ManagedCode.MCPGateway/Internal/Runtime/Core/McpGatewayRuntime.cs Renames lexical feature weight constant to heuristic naming.
src/ManagedCode.MCPGateway/Internal/Runtime/Core/McpGatewayRuntime.Types.cs Renames retrieval candidate score field to heuristic naming.
src/ManagedCode.MCPGateway/Internal/Embeddings/McpGatewayToolEmbeddingStoreIndex.cs Removes the old internal embedding index helper implementation.
src/ManagedCode.MCPGateway/Embeddings/McpGatewayInMemoryToolEmbeddingStore.cs Implements the store using IMemoryCache and supports DI-owned vs self-owned cache lifetimes.
docs/Architecture/Overview.md Updates architecture diagrams/text to reflect IMemoryCache backing and module list cleanup.
docs/ADR/ADR-0004-process-local-embedding-store-uses-imemorycache.md Adds ADR documenting the IMemoryCache decision and invariants.
README.md Updates embedding-store guidance to use AddMcpGatewayInMemoryToolEmbeddingStore() and clarifies process-local scope.
Directory.Packages.props Adds package version for Microsoft.Extensions.Caching.Memory.
AGENTS.md Adds explicit repo guidance favoring framework caching primitives and removing legacy leftovers.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@KSemenenko KSemenenko force-pushed the codex/imemorycache-embedding-store branch from 328f730 to feea947 Compare March 8, 2026 09:25
@KSemenenko
Copy link
Member Author

Addressed the memory-cache review feedback in f35237a.

Changes:

  • set an explicit cache-entry Size for both exact and fallback IMemoryCache writes so the store works with shared caches configured with MemoryCacheOptions.SizeLimit
  • added regression coverage for McpGatewayInMemoryToolEmbeddingStore against a size-limited shared MemoryCache

Verified locally:

  • dotnet restore ManagedCode.MCPGateway.slnx
  • dotnet build ManagedCode.MCPGateway.slnx -c Release --no-restore
  • dotnet build ManagedCode.MCPGateway.slnx -c Release --no-restore -p:RunAnalyzers=true
  • dotnet test --solution ManagedCode.MCPGateway.slnx -c Release --no-build
  • roslynator analyze for src and tests
  • cloc --include-lang=C# src tests

@KSemenenko KSemenenko merged commit cd71dae into codex/mcp-agent-framework-chat-integration Mar 8, 2026
@KSemenenko KSemenenko deleted the codex/imemorycache-embedding-store branch March 8, 2026 09:56
@KSemenenko KSemenenko restored the codex/imemorycache-embedding-store branch March 8, 2026 09:57
@KSemenenko KSemenenko deleted the codex/imemorycache-embedding-store branch March 8, 2026 10:00
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.

2 participants