Skip to content
LogoLogo

How to give the AI agent tools from an app

This guide shows you how to add an MCP server to your app. The Agent panel of the dashboard, the AI agent, can then call the tools of your server in each project that has the app.

You need an app with a server that uses @picked/app-kit, and a public https URL for that server. Build and install your first app makes these.

1. Declare the MCP server

Add mcp to the manifest, with the full URL of the MCP server in url. The URL can be on the host of the endpoint or on a different host. A manifest with mcp does not need an endpoint.

picked.config.ts
import { defineConfig } from '@picked/config/define'
 
export default defineConfig({
  app: {
    handle: 'my-app',
    name: 'My App',
    endpoint: 'https://my-app.example.com',
    scopes: ['organization_leads/read'],
    mcp: { url: 'https://my-app.example.com/mcp' },
  },
})

Add to scopes each scope that your tools need to call the Picked API. Step 4 tells how the tools get a token.

2. Write the MCP server

Write a function that answers one MCP request. It gets the request and the installation that the request is for. It returns a Response.

The server below answers JSON-RPC over the Streamable HTTP transport of MCP. It answers with JSON, and it has one tool.

src/mcp.ts
import { json, type InstallationRef } from '@picked/app-kit/server'
 
const TOOLS = [
  {
    name: 'project_summary',
    description: 'Gives the ID of the project and of the installation.',
    inputSchema: { type: 'object', properties: {} },
  },
]
 
export async function handleMcp(
  request: Request,
  installation: InstallationRef,
): Promise<Response> {
  if (request.method === 'GET') return new Response(null, { status: 405 })
  if (request.method === 'DELETE') return new Response(null, { status: 200 })
 
  const body = await request.json()
  // A notification has no id and gets no answer.
  if (body.id === undefined) return new Response(null, { status: 202 })
 
  const answer = (result: unknown): Response => json({ jsonrpc: '2.0', id: body.id, result })
  switch (body.method) {
    case 'initialize':
      return answer({
        protocolVersion: body.params?.protocolVersion ?? '2025-06-18',
        capabilities: { tools: {} },
        serverInfo: { name: 'my-app', version: '1.0.0' },
      })
    case 'ping':
      return answer({})
    case 'tools/list':
      return answer({ tools: TOOLS })
    case 'tools/call':
      if (body.params?.name !== 'project_summary') {
        return answer({ isError: true, content: [{ type: 'text', text: 'Unknown tool' }] })
      }
      return answer({
        content: [
          {
            type: 'text',
            text: JSON.stringify({
              projectId: installation.projectId,
              installationId: installation.installationId,
            }),
          },
        ],
      })
    default:
      return json({
        jsonrpc: '2.0',
        id: body.id,
        error: { code: -32601, message: 'Method not found' },
      })
  }
}

The example app in the Picked repository, fixtures/app-lead-insights, has a full server with two tools in src/mcp.ts.

3. Give the server to the handler

Give createAppHandler an mcp option. Its path is the path of mcp.url in the manifest, /mcp in this example:

src/server.ts
import { createAppHandler, readAppEnvironment } from '@picked/app-kit/server'
import { serve } from '@picked/app-kit/node'
import { handleMcp } from './mcp'
 
const port = process.env.APP_PORT ?? '4103'
const { pickedKeys } = readAppEnvironment(process.env, port)
 
const handler = createAppHandler({
  pickedKeys,
  mcp: { path: '/mcp', handle: handleMcp },
})
 
serve(handler, Number(port))

For each request to path, createAppHandler checks the picked-signature header. This header signs the picked-context header together with the body. If the check fails, it answers 401 with { "error": "invalid_signature" }, and does not call handle. If the check passes, it reads the installation from picked-context and calls handle.

The installation comes only from picked-context. The MCP body does not identify it.

4. Call Picked from a tool

The installation that handle gets has a token. The token expires after one hour, and it has the scopes of the installation. To call the Picked API with it, make an installation client:

src/leads.ts
import { graphql, type ResultOf } from '@picked/admin-api-client'
import {
  installationClientFor,
  readAppEnvironment,
  type InstallationRef,
} from '@picked/app-kit/server'
 
const { privateKey } = readAppEnvironment(process.env, process.env.APP_PORT ?? '4103')
 
const RecentLeadsDocument = graphql(`
  query RecentLeads($projectId: ID!) {
    project(input: { id: $projectId }) {
      leads(first: 10) {
        nodes {
          id
          createdAt
        }
      }
    }
  }
`)
 
export async function recentLeads(
  installation: InstallationRef,
): Promise<ResultOf<typeof RecentLeadsDocument>> {
  const client = installationClientFor(installation, { privateKey })
  return client.request(RecentLeadsDocument, { projectId: installation.projectId })
}

graphql comes from the @picked/admin-api-client package.

Call recentLeads(installation) in the tools/call branch of handleMcp, and put the result in the text of the answer.

Return an error from a tool as a result with isError: true. Then the AI agent can read the message.

How to read project data from an app tells more about the installation client.

5. Deploy and try the server

  1. Deploy a new version of the manifest.
  2. Upgrade the installation to the new version.
  3. In the project, open the Agent panel.
  4. Ask for a task that a tool of your server does.

If the project does not have the app, install it in place of step 2.

The Agent panel finds the MCP server of each app that the project has. You do not add the server to the project. The panel shows each server by the name of the app.

The person must hold organization_apps/read on the project. Picked sends no request from a person without it.

If the panel cannot reach your server, it shows Couldn't reach <app name>: <message>.