Custom Headers for MCP

Learn how to add custom headers to group tool calls together.

Tool calls can be grouped together using custom headers. This can be used to group tool calls that are part of the same chat session, same product, or any other custom grouping.

Adding custom headers

Merge Agent Handler will register any header with a prefix of X- as a custom header, and will store that information. We’ll associate the header with the given tool call. These headers will be visible in the logs dashboard, and filterable using the Request Headers filter.

Example code snippet:

1async def connect_to_mcp():
2 """Connect to an MCP server and use its tools"""
3 from mcp.client.streamable_http import streamablehttp_client
4 from mcp import ClientSession
5
6 # Custom headers for authentication and other requirements
7 headers = {
8 "Authorization": "Bearer <auth_token>",
9 "Mcp-Session-Id": "session-" + str(asyncio.current_task().get_name()),
10 "X-chat-session-id": "chat-" + str(asyncio.current_task().get_name())
11 }
12
13 # MCP server URL
14 server_url = "https://ah-api.merge.dev/api/v1/tool-packs/<tool_pack_id>/registered-users/<registered_user_id>/mcp"

Example headers:

  • X-chat-session-id: used to group tool calls that are part of the same end-user chat session
  • X-workflow-id: used to group tool calls that are part of the same workflow
  • X-product-id: used to group tool calls from that were called from the same product

Within the logs dashboard, you can filter on any custom header sent to Merge Agent Handler.