import o from"@contractspec/lib.ui-link";import{ChevronRight as a}from"lucide-react";import{jsx as e,jsxs as t}from"react/jsx-runtime";function n(){return t("div",{className:"space-y-8",children:[t("div",{className:"space-y-4",children:[e("h1",{className:"font-bold text-4xl",children:"MCP Adapters"}),t("p",{className:"text-muted-foreground",children:["The ",e("strong",{children:"Model Context Protocol (MCP)"})," is an open standard for connecting AI models to external tools and data sources. ContractSpec provides MCP adapters that allow you to expose your capabilities as MCP tools and consume external MCP servers as capabilities."]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Why MCP integration matters"}),e("p",{className:"text-muted-foreground",children:"AI agents need access to real-world tools and data to be useful. MCP provides a standardized way to expose these capabilities. By integrating ContractSpec with MCP, you can:"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[e("li",{children:"Let AI agents invoke your ContractSpec capabilities safely and securely"}),e("li",{children:"Use external MCP servers (databases, APIs, search engines) as capability providers in your workflows"}),e("li",{children:"Build AI-powered features without writing custom integration code"}),e("li",{children:"Enforce policies on AI agent actions just like any other user"})]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Exposing capabilities as MCP tools"}),t("p",{className:"text-muted-foreground",children:["Any"," ",e(o,{href:"/docs/specs/capabilities",className:"text-violet-400 hover:text-violet-300",children:"CapabilitySpec"})," ","can be automatically exposed as an MCP tool. ContractSpec generates:"]}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[e("li",{children:"A tool schema describing the capability's inputs, outputs, and purpose"}),e("li",{children:"An MCP server endpoint that AI agents can connect to"}),e("li",{children:"Policy enforcement ensuring agents can only invoke capabilities they're authorized to use"}),e("li",{children:"Audit logging of all agent actions for compliance and debugging"})]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Example: Exposing a capability"}),e("p",{className:"text-muted-foreground",children:"Here's how to expose ContractSpec operations as MCP tools:"}),e("div",{className:"overflow-x-auto rounded-lg border border-border bg-background/50 p-4 font-mono text-muted-foreground text-sm",children:e("pre",{children:`import { McpServer } from '@modelcontextprotocol/sdk/server/mcp.js';
import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js';
import { createMcpServer } from '@contractspec/lib.contracts-runtime-server-mcp/provider-mcp';
import { registry, resources, prompts } from './lib/registry';

const server = new McpServer({
  name: 'my-app-mcp',
  version: '1.0.0',
});

createMcpServer(server, registry, resources, prompts, {
  toolCtx: () => ({ actor: 'ai-agent' }),
  promptCtx: () => ({ userId: null, orgId: null }),
  resourceCtx: () => ({ userId: null, orgId: null }),
});

const transport = new StdioServerTransport();
await server.connect(transport);`})}),e("p",{className:"text-muted-foreground",children:"AI agents can now discover and invoke your operations through the MCP protocol. ContractSpec handles authentication, authorization, and execution automatically via the `OperationSpecRegistry`."})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Consuming external MCP servers"}),e("p",{className:"text-muted-foreground",children:"You can integrate external MCP servers into your workflows. Define an integration spec and use it in your operations:"}),e("div",{className:"overflow-x-auto rounded-lg border border-border bg-background/50 p-4 font-mono text-muted-foreground text-sm",children:e("pre",{children:`import { defineIntegration } from '@contractspec/lib.contracts-integrations';
import { SchemaModel, ScalarTypeEnum } from '@contractspec/lib.schema';

const SearchProductsIntegration = defineIntegration({
  meta: {
    key: 'products.search',
    version: '1.0.0',
    category: 'mcp',
    description: 'Search products via external MCP server',
  },
  connection: {
    serverUrl: 'https://api.example.com/mcp',
    toolName: 'search_products',
  },
  io: {
    input: new SchemaModel({
      name: 'SearchProductsInput',
      fields: {
        query: { type: ScalarTypeEnum.String(), isOptional: false },
        category: { type: ScalarTypeEnum.String(), isOptional: true },
      },
    }),
    output: new SchemaModel({
      name: 'ProductResults',
      fields: {
        products: { type: ScalarTypeEnum.JSON(), isOptional: false, isArray: true },
      },
    }),
  },
});`})})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Policy enforcement for AI agents"}),t("p",{className:"text-muted-foreground",children:["AI agents are treated like any other actor in ContractSpec. They must authenticate, and all their actions are subject to"," ",e(o,{href:"/docs/specs/policy",className:"text-violet-400 hover:text-violet-300",children:"PolicySpecs"}),". You can define specific policies in TypeScript:"]}),e("div",{className:"overflow-x-auto rounded-lg border border-border bg-background/50 p-4 font-mono text-muted-foreground text-sm",children:e("pre",{children:`import { definePolicy } from '@contractspec/lib.contracts-spec';

export const AIAgentRestrictions = definePolicy({
  meta: {
    key: 'ai.agent.restrictions',
    version: '1.0.0',
    description: 'Restrict AI agent actions',
  },
  rules: [
    {
      id: 'agents-read-only',
      effect: 'DENY',
      condition: (ctx) => ctx.actor === 'ai-agent' && ctx.action === 'write',
    },
    {
      id: 'agents-require-approval',
      effect: 'DENY',
      condition: (ctx, spec) => 
        ctx.actor === 'ai-agent' && 
        spec.policy.escalate === 'human_review' &&
        !ctx.approvalGranted,
    },
  ],
});`})})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Human-in-the-loop workflows"}),e("p",{className:"text-muted-foreground",children:"For sensitive operations, you can require human approval before an AI agent can proceed. ContractSpec provides built-in approval workflows:"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[e("li",{children:"Agent requests permission to invoke a capability"}),e("li",{children:"Request is sent to designated approvers (via email, Slack, etc.)"}),e("li",{children:"Approver reviews the request and approves or denies it"}),e("li",{children:"Agent receives the decision and can proceed if approved"})]}),t("p",{className:"text-muted-foreground",children:["All approval decisions are logged in the"," ",e(o,{href:"/docs/safety/auditing",className:"text-violet-400 hover:text-violet-300",children:"audit log"}),"."]})]}),t("div",{className:"space-y-4",children:[e("h2",{className:"font-bold text-2xl",children:"Best practices"}),t("ul",{className:"list-inside list-disc space-y-2 text-muted-foreground",children:[e("li",{children:"Start with read-only capabilities for AI agents—only grant write access when necessary."}),e("li",{children:"Use human-in-the-loop approval for any capability that modifies critical data or triggers financial transactions."}),e("li",{children:"Set rate limits to prevent runaway agents from overwhelming your system."}),e("li",{children:"Provide clear, detailed descriptions for each capability so agents know when to use them."}),e("li",{children:"Monitor agent actions closely in production and adjust policies as needed."}),e("li",{children:"Test agent integrations thoroughly with realistic scenarios before deploying."})]})]}),t("div",{className:"flex items-center gap-4 pt-4",children:[e(o,{href:"/docs/advanced/renderers",className:"btn-ghost",children:"Previous: Custom Renderers"}),t(o,{href:"/docs/advanced/telemetry",className:"btn-primary",children:["Next: Telemetry ",e(a,{size:16})]})]})]})}export{n as AdvancedMCPPage};
