McpToolRegistrationService class
Discover MCP servers and list tools formatted for the LangChain Orchestrator. Uses listToolServers to fetch server configs and getTools to enumerate tools.
Also provides methods to send chat history to the MCP platform for real-time threat protection (RTP) analysis.
Constructors
| Mcp |
Construct a McpToolRegistrationService. |
Methods
| add |
Registers MCP tool servers and updates agent options with discovered tools and server configs. Call this to enable dynamic LangChain tool access based on the current MCP environment. |
| send |
Sends chat history from a LangGraph CompiledStateGraph to the MCP platform. This is the highest-level and easiest-to-use API. It retrieves the current state from the graph, extracts messages, converts them to ChatHistoryMessage format, and sends them to the MCP platform for real-time threat protection. Example
|
| send |
Retrieves messages from a BaseChatMessageHistory instance and sends them to the MCP platform. Use this API when working with LangChain's memory abstractions (e.g., InMemoryChatMessageHistory, RedisChatMessageHistory, etc.). Example
|
| send |
Sends an array of LangChain messages to the MCP platform for real-time threat protection. This is the lowest-level API that accepts raw BaseMessage arrays. Use this when you have already extracted messages or have a custom message source not covered by the higher-level APIs. This method converts the provided BaseMessage array to ChatHistoryMessage format and sends them to the MCP platform. Empty arrays are sent as-is to register the user message with the platform. Example
|
| send |
Extracts messages from a LangGraph StateSnapshot and sends them to the MCP platform. Use this API when you already have a StateSnapshot (e.g., from a previous
Example
|
Constructor Details
McpToolRegistrationService(IConfigurationProvider<LangChainToolingConfiguration>)
Construct a McpToolRegistrationService.
new McpToolRegistrationService(configProvider?: IConfigurationProvider<LangChainToolingConfiguration>)
Parameters
- configProvider
Optional configuration provider. Defaults to defaultLangChainToolingConfigurationProvider if not specified.
Method Details
addToolServersToAgent(ReactAgent<AgentTypeConfig<Record<string, any>, undefined, AnyAnnotationRoot, readonly AgentMiddleware<any, any, any, readonly (ClientTool | ServerTool)[]>[], readonly (ClientTool | ServerTool)[]>>, Authorization, string, TurnContext, string)
Registers MCP tool servers and updates agent options with discovered tools and server configs. Call this to enable dynamic LangChain tool access based on the current MCP environment.
function addToolServersToAgent(agent: ReactAgent<AgentTypeConfig<Record<string, any>, undefined, AnyAnnotationRoot, readonly AgentMiddleware<any, any, any, readonly (ClientTool | ServerTool)[]>[], readonly (ClientTool | ServerTool)[]>>, authorization: Authorization, authHandlerName: string, turnContext: TurnContext, authToken: string): Promise<ReactAgent<AgentTypeConfig<Record<string, any>, undefined, AnyAnnotationRoot, readonly AgentMiddleware<any, any, any, readonly (ClientTool | ServerTool)[]>[], readonly (ClientTool | ServerTool)[]>>>
Parameters
- agent
-
ReactAgent<AgentTypeConfig<Record<string, any>, undefined, AnyAnnotationRoot, readonly AgentMiddleware<any, any, any, readonly (ClientTool | ServerTool)[]>[], readonly (ClientTool | ServerTool)[]>>
The LangChain Agent instance to which MCP servers will be added.
- authorization
- Authorization
Authorization object for token exchange.
- authHandlerName
-
string
The name of the auth handler to use for token exchange.
- turnContext
- TurnContext
The TurnContext of the current request.
- authToken
-
string
Optional bearer token for MCP server access.
Returns
Promise<ReactAgent<AgentTypeConfig<Record<string, any>, undefined, AnyAnnotationRoot, readonly AgentMiddleware<any, any, any, readonly (ClientTool | ServerTool)[]>[], readonly (ClientTool | ServerTool)[]>>>
The updated Agent instance with registered MCP servers.
sendChatHistoryAsync(TurnContext, CompiledStateGraph<unknown, unknown, string, StateDefinition, StateDefinition, StateDefinition, unknown, unknown, unknown>, RunnableConfig<Record<string, any>>, number, ToolOptions)
Sends chat history from a LangGraph CompiledStateGraph to the MCP platform.
This is the highest-level and easiest-to-use API. It retrieves the current state from the graph, extracts messages, converts them to ChatHistoryMessage format, and sends them to the MCP platform for real-time threat protection.
Example
const config = { configurable: { thread_id: '1' } };
const result = await service.sendChatHistoryAsync(turnContext, graph, config);
if (result.succeeded) {
console.log('Chat history sent successfully');
}
function sendChatHistoryAsync(turnContext: TurnContext, graph: CompiledStateGraph<unknown, unknown, string, StateDefinition, StateDefinition, StateDefinition, unknown, unknown, unknown>, config: RunnableConfig<Record<string, any>>, limit?: number, toolOptions?: ToolOptions): Promise<OperationResult>
Parameters
- turnContext
- TurnContext
The turn context containing conversation information.
- graph
-
CompiledStateGraph<unknown, unknown, string, StateDefinition, StateDefinition, StateDefinition, unknown, unknown, unknown>
The LangGraph CompiledStateGraph instance. The graph state must contain a 'messages' array.
- config
-
RunnableConfig<Record<string, any>>
The RunnableConfig containing thread_id and other configuration.
- limit
-
number
Optional limit on the number of messages to send.
- toolOptions
- ToolOptions
Optional tool options for customization.
Returns
Promise<OperationResult>
A Promise resolving to an OperationResult indicating success or failure.
sendChatHistoryFromChatHistoryAsync(TurnContext, BaseChatMessageHistory, number, ToolOptions)
Retrieves messages from a BaseChatMessageHistory instance and sends them to the MCP platform.
Use this API when working with LangChain's memory abstractions (e.g., InMemoryChatMessageHistory, RedisChatMessageHistory, etc.).
Example
const chatHistory = new InMemoryChatMessageHistory();
// ... add messages to history ...
const result = await service.sendChatHistoryFromChatHistoryAsync(turnContext, chatHistory);
function sendChatHistoryFromChatHistoryAsync(turnContext: TurnContext, chatHistory: BaseChatMessageHistory, limit?: number, toolOptions?: ToolOptions): Promise<OperationResult>
Parameters
- turnContext
- TurnContext
The turn context containing conversation information.
- chatHistory
-
BaseChatMessageHistory
The BaseChatMessageHistory instance to retrieve messages from.
- limit
-
number
Optional limit on the number of messages to send.
- toolOptions
- ToolOptions
Optional tool options for customization.
Returns
Promise<OperationResult>
A Promise resolving to an OperationResult indicating success or failure.
sendChatHistoryFromMessagesAsync(TurnContext, BaseMessage<MessageStructure<MessageToolSet>, MessageType>[], number, ToolOptions)
Sends an array of LangChain messages to the MCP platform for real-time threat protection.
This is the lowest-level API that accepts raw BaseMessage arrays. Use this when you have already extracted messages or have a custom message source not covered by the higher-level APIs.
This method converts the provided BaseMessage array to ChatHistoryMessage format and sends them to the MCP platform. Empty arrays are sent as-is to register the user message with the platform.
Example
const messages = await messageHistory.getMessages();
const result = await service.sendChatHistoryFromMessagesAsync(turnContext, messages, 50);
if (result.succeeded) {
console.log('Chat history sent successfully');
} else {
console.error('Failed to send chat history:', result.errors);
}
function sendChatHistoryFromMessagesAsync(turnContext: TurnContext, messages: BaseMessage<MessageStructure<MessageToolSet>, MessageType>[], limit?: number, toolOptions?: ToolOptions): Promise<OperationResult>
Parameters
- turnContext
- TurnContext
The turn context containing conversation information.
- messages
-
BaseMessage<MessageStructure<MessageToolSet>, MessageType>[]
Array of LangChain BaseMessage objects to send.
- limit
-
number
Optional limit on the number of messages to send.
- toolOptions
- ToolOptions
Optional tool options for customization.
Returns
Promise<OperationResult>
A Promise resolving to an OperationResult indicating success or failure.
sendChatHistoryFromStateAsync(TurnContext, StateSnapshot, number, ToolOptions)
Extracts messages from a LangGraph StateSnapshot and sends them to the MCP platform.
Use this API when you already have a StateSnapshot (e.g., from a previous
graph.getState() call) and want to avoid fetching state again.
Example
const config = { configurable: { thread_id: '1' } };
const stateSnapshot = await graph.getState(config);
const result = await service.sendChatHistoryFromStateAsync(turnContext, stateSnapshot);
function sendChatHistoryFromStateAsync(turnContext: TurnContext, stateSnapshot: StateSnapshot, limit?: number, toolOptions?: ToolOptions): Promise<OperationResult>
Parameters
- turnContext
- TurnContext
The turn context containing conversation information.
- stateSnapshot
-
StateSnapshot
The LangGraph StateSnapshot containing message state.
- limit
-
number
Optional limit on the number of messages to send.
- toolOptions
- ToolOptions
Optional tool options for customization.
Returns
Promise<OperationResult>
A Promise resolving to an OperationResult indicating success or failure.