Skip to content
Source
Sign inSign up

MCP servers and secrets

Software factory / Custom agents

MCP servers and secrets

Your custom agent needs data from another service. For example, it links each new bug report to the Linear ticket for it, through Linear's MCP server, with your Linear key.

3 min read

Give the agent the key as a secret, and the service's host as egress. The sandbox gets a placeholder, not the key. The forge puts the real key in place of the placeholder only in requests to the hosts of the secret.

Tip

Is the MCP server already connected to the repository? Then the agent needs no MCP client and no secret of its own. See Use a connected MCP server.

You need:

1. Add the key as a CI secret

A custom agent uses the CI secrets of the repository. Bind the secret to the host of the service. A secret with no host gets no placeholder.

src secret set LINEAR_KEY --env development --host mcp.linear.app --header Authorization < linear-key.txt

A custom agent takes its secrets from the development CI environment. It gets them only when its run's trust level is at least the environment's lowest trust level (trusted by default): on issues and pull requests of people with write access. On the items of other people, it gets no secret. See Trust levels and secrets.

2. Name the secret and the host in the agent

Add secrets and egress to the agent's forge fields, and connect the MCP server in run:

import { defineFactory } from '@tanstack-source/ci'
import { forgeTools } from '@tanstack-source/ci/agent'
import { chat, defineAgent } from '@tanstack/ai'
import { createMCPClient } from '@tanstack/ai-mcp'

export default defineFactory('house', {
  agents: {
    'link-tickets': {
      agent: defineAgent({
        name: 'link-tickets',
        description: 'Links each new bug report to its Linear ticket.',
        run: async (ctx) => {
          // A placeholder: the forge swaps it for the key in requests to mcp.linear.app only.
          const linear = await createMCPClient({
            transport: { type: 'http', url: 'https://mcp.linear.app/mcp', headers: { authorization: `Bearer ${process.env.LINEAR_KEY}` } },
            prefix: 'linear',
          })
          return chat({
            adapter: ctx.forge!.model(),
            systemPrompts: ['Find the Linear ticket of this bug report. Post one comment with its link.'],
            tools: forgeTools(ctx, ['comment', 'record_verdict']),
            mcp: { clients: [linear] },
            messages: ctx.messages,
            ...ctx.forward,
          })
        },
      }),
      on: ['issue.opened'],
      trigger: { on: 'everyone' },
      secrets: ['LINEAR_KEY'],
      egress: ['mcp.linear.app'],
    },
  },
})

The sandbox has each secret as an environment variable with the same name. Its value is a placeholder that starts with tss_ph_.

3. Save the file and see it run

  1. Save the file as in Save the file in the vault.
  2. Open a new issue.

Its Factory panel shows the run of link-tickets, and the issue gets a comment with the link.

Done

Your agent reads Linear through its MCP server, and the key never enters the sandbox.

What the forge allows

  • The forge swaps a placeholder for its value only in the header of the secret, in a request to a host of the secret. If an admin turned on --in-body for the secret, the forge also swaps it in a JSON or form body to such a host.
  • A placeholder in a URL, in another header, or to another host is refused, and the job's error names the secret.
  • The agent reaches only the hosts in egress, on port 443.
  • The forge reads the values when the run first sends a placeholder, after it checks the rules of development again. If an admin raised its lowest trust level above the run's, the request is refused with 403.
  • The values are never stored with the run.

Use a connected MCP server

A custom agent can also use an MCP server that is connected to the repository, such as Linear. These tools are not in tools. The connection picks the agent:

  1. Open the repository, then Agents, then MCP servers under Tools.
  2. On the card of the connection, under Agents, select the agent by its name, for example label-bugs.
  3. Turn on the tools that it needs, then click Save agents and tools.

The agent sees the tools as <connection>_<tool>, for example linear_list_issues. Say in the prompt when to use them. See Give your factory an MCP server.

With the CLI, the id of a custom agent is custom:<name>:

src mcp tools linear --agent triage --agent custom:label-bugs

--agent replaces the agents of the connection, so name each agent that must keep it.