Skip to content

💉 feat: Optionally Inject MCP Server Instructions #7660

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

Merged
merged 3 commits into from
Jun 6, 2025

Conversation

benverhees
Copy link
Contributor

@benverhees benverhees commented May 31, 2025

Summary

This PR introduces functionality to dynamically inject MCP server instructions into the system context of an agent. This supports both ephemeral agents and standard agents with MCP tool associations. The instructions are retrieved from the MCP server through the client API in the MCPManager and injected in the system content.

MCP server instructions provide high-level context about how to effectively use an entire MCP server - including usage patterns, workflows, and general guidance about the server's capabilities. This is distinct from tool descriptions, which are the specific function-level documentation that describes individual tools' parameters and return values.

This change ensures that comprehensive server-level instructions are automatically available to the agent at runtime, complementing the existing tool descriptions with broader contextual guidance. This improves overall tool usability by providing both the "what" (individual tool descriptions) and the "how" (server usage instructions), reducing the need for hardcoded context-specific behavior.

The serverInstructions configuration option provides unified control over instruction behavior:

  • Not set/undefined (default): No instructions will be included
  • true: Use server-provided instructions
  • string: Use custom instructions (overrides server-provided)
  • false: Explicitly disable (same as not setting)

When to Use Each Option

serverInstructions: true

Use when the server provides good default instructions:

  • Well-documented servers: The server provides comprehensive tool usage guidance
  • Standard workflows: The server's instructions match your typical use cases
  • Quick setup: You want to get started without writing custom instructions

serverInstructions: "custom instructions"

Use when you need specific guidance:

  • Tailored workflows: Instructions specific to your application or domain
  • Enhanced clarity: You can write clearer instructions than the server provides
  • Missing context: The server instructions lack important details for your use case
  • Compliance requirements: You need specific formatting or safety guidelines

serverInstructions: false or not set (default)

Use when you prefer minimal context:

  • Simple tools: The tools are self-explanatory and don't need guidance
  • Context optimization: You want to minimize token usage in conversations
  • Manual control: You prefer to provide guidance through conversation rather than system context

Change Type

Please delete any irrelevant options.

  • New feature (non-breaking change which adds functionality)

Testing

This feature was manually tested by triggering both ephemeral and standard agents with associated MCP tools and verifying that:

  • Relevant MCP instructions are injected into the context.
  • No instructions are injected if none are available or applicable.
  • Proper logging appears for debugging and observability.
  • The agent’s systemContent reflects the expected formatting.

Test Configuration:

Environment: Local development with mocked MCP server connections and instructions

Checklist

Please delete any irrelevant options.

  • My code adheres to this project's style guidelines
  • I have performed a self-review of my own code
  • I have commented in any complex areas of my code
  • I have made pertinent documentation changes
  • My changes do not introduce new warnings
  • I have written tests demonstrating that my changes are effective or that my feature works
  • Local unit tests pass with my changes
  • Any changes dependent on mine have been merged and published in downstream modules.
  • A pull request for updating the documentation has been submitted.

@benverhees benverhees force-pushed the feature/mcp-instructions branch from c6e7863 to b0c64a1 Compare May 31, 2025 23:06
@danny-avila danny-avila marked this pull request as draft June 1, 2025 21:43
@benverhees
Copy link
Contributor Author

@danny-avila I see you marked this PR as work in progress, is there anything that needs to be done to move this forward?

@danny-avila
Copy link
Owner

Hi @benverhees

Yes, in my opinion, this should be a configurable pattern, rather than including instructions every time. In fact, I would even make the instructions configurable. The function definitions are already adding a lot to the context.

@benverhees benverhees force-pushed the feature/mcp-instructions branch from b0c64a1 to cbf690b Compare June 2, 2025 14:59
@benverhees
Copy link
Contributor Author

benverhees commented Jun 2, 2025

@danny-avila I have added a serverInstructions attribute to the MCPOptionsSchema which can be configured as a string or a boolean.

The serverInstructions configuration option provides unified control over instruction behavior:

  • Not set/undefined (default): No instructions will be included
  • true: Use server-provided instructions
  • string: Use custom instructions (overrides server-provided)
  • false: Explicitly disable (same as not setting)

When to Use Each Option

serverInstructions: true

Use when the server provides good default instructions:

  • Well-documented servers: The server provides comprehensive tool usage guidance
  • Standard workflows: The server's instructions match your typical use cases
  • Quick setup: You want to get started without writing custom instructions

serverInstructions: "custom instructions"

Use when you need specific guidance:

  • Tailored workflows: Instructions specific to your application or domain
  • Enhanced clarity: You can write clearer instructions than the server provides
  • Missing context: The server instructions lack important details for your use case
  • Compliance requirements: You need specific formatting or safety guidelines

serverInstructions: false or not set (default)

Use when you prefer minimal context:

  • Simple tools: The tools are self-explanatory and don't need guidance
  • Context optimization: You want to minimize token usage in conversations
  • Manual control: You prefer to provide guidance through conversation rather than system context

@benverhees benverhees force-pushed the feature/mcp-instructions branch 2 times, most recently from a6add81 to ff25bc5 Compare June 2, 2025 19:25
@benverhees
Copy link
Contributor Author

@danny-avila if this looks good to you, I'll make a pull request in the docs repo for this.

@benverhees benverhees marked this pull request as ready for review June 4, 2025 19:17
@benverhees benverhees force-pushed the feature/mcp-instructions branch from ff25bc5 to b02de73 Compare June 6, 2025 06:21
@benverhees
Copy link
Contributor Author

@danny-avila is this something you would consider merging, or are there still some things that need improvement?

@danny-avila danny-avila changed the base branch from main to dev June 6, 2025 20:47
@danny-avila danny-avila changed the title feat: Inject MCP Server instructions into agent instructions 💉 feat: Optionally Inject MCP Server Instructions Jun 6, 2025
@danny-avila danny-avila requested a review from Copilot June 6, 2025 20:48
@danny-avila
Copy link
Owner

@benverhees this is definitely aligned to what I had in mind, thank you! Reviewing now.

Copilot

This comment was marked as outdated.

danny-avila and others added 2 commits June 6, 2025 17:09
@danny-avila danny-avila requested a review from Copilot June 6, 2025 21:19
Copy link
Contributor

@Copilot 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 introduces the optional injection of MCP server instructions into the agent’s system context, supporting both ephemeral agents and standard agents with MCP tool associations. Key changes include:

  • Adding new logic in MCPManager to store, retrieve, and format MCP server instructions.
  • Updating the configuration schema in the data-provider to include the new serverInstructions option.
  • Modifying the agent client to extract MCP server names and inject formatted instructions into the system context.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/mcp/src/manager.ts Implements storage, retrieval, and formatting of server instructions
packages/data-provider/src/mcp.ts Updates configuration schema with the serverInstructions field
api/server/controllers/agents/client.js Incorporates injection of MCP instructions into agent systemContent
Comments suppressed due to low confidence (1)

api/server/controllers/agents/client.js:376

  • [nitpick] Consider renaming 'mcpServers' to 'mcpServerNames' to better indicate that it holds server name values, which may improve clarity.
let mcpServers = [];

@danny-avila
Copy link
Owner

Looks good to me, I will merge to the dev branch but would kindly ask if appropriate docs could be updated:

https://github.com/LibreChat-AI/librechat.ai/blob/main/pages/docs/configuration/librechat_yaml/object_structure/mcp_servers.mdx

@danny-avila danny-avila merged commit 2c39ccd into danny-avila:dev Jun 6, 2025
7 checks passed
@benverhees
Copy link
Contributor Author

Looks good to me, I will merge to the dev branch but would kindly ask if appropriate docs could be updated:

https://github.com/LibreChat-AI/librechat.ai/blob/main/pages/docs/configuration/librechat_yaml/object_structure/mcp_servers.mdx

Thanks! I will create a PR in the docs repo!

@benverhees
Copy link
Contributor Author

@danny-avila documentation is in LibreChat-AI/librechat.ai#321

jmaddington pushed a commit to jmaddington/LibreChat that referenced this pull request Jun 17, 2025
* feat: Add MCP server instructions to context

* chore: remove async method as no async code is performed

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>

* chore: remove co-pilot promise resolution

---------

Co-authored-by: Danny Avila <danacordially@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
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