Open Closed

Tenant id from MCP server (ABP AI Workspace) #10953


User avatar
0
marco.susani created
  • Template: app-nolayers
  • Template Type: Modern
  • Created ABP Studio Version: 3.1.1
  • Current ABP Studio Version: 3.1.6
  • Multi-Tenancy: Yes
  • UI Framework: react
  • Theme: leptonx
  • Theme Style: system
  • Database Provider: ef
  • Database Management System: sqlserver
  • Public Website: Yes
  • Social Login: Yes
  • Include Tests: Yes
  • Use Local References: No
  • Optional Modules:
    • GDPR
    • TextTemplateManagement
    • AuditLogging
    • OpenIddictAdmin
    • LowCode
    • Elsa
  • Selected Languages: Italian, English
  • AI Management Enabled: Yes
  • Selected AI Providers: OpenAI
  • Default Language: Italian
  • Create Command: abp new Nicma.Idp -t app-nolayers --modern --modular --ui-framework react --database-provider ef --database-management-system sqlserver --theme leptonx --tests --public-website --without-cms-kit --dont-run-install-libs --dont-run-bundling -no-file-management -no-language-management -low-code --elsa

Hello,

how can i use the current tenant id in a custom server MCP; AI Workspace ABP pass it in header?

My server MCP is a streamable HTTP.

Markdown supported.
Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)

3 Answer(s)
  • User Avatar
    0
    AI-Bot created
    Support Team ABP Agent
    • ABP’s React UI (modern templates) keeps the selected tenant id in sessionStorage under abp_tenant_id and sends it to the backend with the __tenant HTTP header on requests. So, if your MCP server is a custom, separate “streamable HTTP” endpoint, ABP AI Workspace does not automatically forward the current tenant to that external MCP server unless you explicitly implement such forwarding in your MCP server integration.
    • Therefore, to use the current tenant inside your custom MCP server, you should read it from the incoming request header __tenant (or, if you are calling your MCP server from your React app directly, make your client include __tenant header using the value from sessionStorage['abp_tenant_id']).

    Practical approach (custom streamable HTTP MCP server)

    1. Expect this header on your MCP server:

      • __tenant: <tenantId> (GUID as string)
    2. In ASP.NET Core (example), read it like:

    app.MapPost("/mcp", async (HttpContext httpContext) =>
    {
        var tenantId = httpContext.Request.Headers["__tenant"].ToString(); // may be empty for Host
        // use tenantId in your logic
    });
    
    1. If the MCP server is called from your React UI, add __tenant header from:
    • sessionStorage.getItem("abp_tenant_id")

    Sources

    • https://abp.io/docs/10.5/framework/ui/react/permission-management (validated)
    • https://abp.io/docs/10.2/modules/ai-management (validated)

    This response was generated by AI. If it didn’t solve your problem, feel free to post a new answer — our support team will jump in to help you soon.

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    marco.susani created

    My server MPC is a Node application, and i tried to get tenant id from header, but it's undefined:

    const tenant = ((req?.headers["__tenant"] as string) || process.env.ABP_TENANT);

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
  • User Avatar
    0
    maliming created
    Support Team Fullstack Developer

    Hi,

    AI Workspace doesn't forward the current tenant id to MCP servers. The headers you set on the MCP server in AI Management are shared by all tenants, so they can't carry it either.

    You can add the header yourself with a message handler on the HttpClient that AI Management uses for your MCP server. The client name is McpClient_ + the Name of your MCP server in AI Management.

    using System.Net.Http;
    using System.Threading;
    using System.Threading.Tasks;
    using Volo.Abp.DependencyInjection;
    using Volo.Abp.MultiTenancy;
    
    namespace Nicma.Idp;
    
    public class McpTenantHeaderHandler : DelegatingHandler, ITransientDependency
    {
        private readonly ICurrentTenant _currentTenant;
    
        public McpTenantHeaderHandler(ICurrentTenant currentTenant)
        {
            _currentTenant = currentTenant;
        }
    
        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            request.Headers.Remove("__tenant");
            if (_currentTenant.Id.HasValue)
            {
                request.Headers.TryAddWithoutValidation("__tenant", _currentTenant.Id.Value.ToString());
            }
    
            return base.SendAsync(request, cancellationToken);
        }
    }
    

    Then register it in ConfigureServices of your host module:

    context.Services.AddHttpClient("McpClient_<your-mcp-server-name>")
        .AddHttpMessageHandler<McpTenantHeaderHandler>();
    

    When there is no current tenant (host), the header isn't sent. In that case your Node code falls back to process.env.ABP_TENANT, so remove that fallback if host calls shouldn't use a fixed tenant.

    AI Management reuses the same MCP session for all tenants of that server. So read __tenant from each tool call's request, not from the request that started the session.

    Thanks

    Markdown supported.
    Copy, paste, or drag & drop images and files (max 100 MB per file, 100 MB total per post)
Boost Your Development
ABP Live Training
Packages
See Trainings
Mastering ABP Framework Book
The Official Guide
Mastering
ABP Framework
Learn More
Mastering ABP Framework Book
Made with ❤️ on ABP v10.8.0-preview. Updated on November 02, 2026, 06:56
1
ABP Assistant
🔐 You need to be logged in to use the chatbot. Please log in first.