|
| 1 | +--- |
| 2 | +title: "Connect to MCP Servers" |
| 3 | +description: "Integrate Model Context Protocol (MCP) servers into React applications" |
| 4 | +icon: "lucide/Network" |
| 5 | +--- |
| 6 | + |
| 7 | +import { |
| 8 | + TailoredContent, |
| 9 | + TailoredContentOption, |
| 10 | +} from "@/components/react/tailored-content.tsx"; |
| 11 | +import Link from "next/link"; |
| 12 | +import { |
| 13 | + Database, |
| 14 | + Code, |
| 15 | + Server, |
| 16 | + Bot, |
| 17 | + SquareTerminal, |
| 18 | + SquareChartGantt, |
| 19 | +} from "lucide-react"; |
| 20 | + |
| 21 | +## Introduction |
| 22 | + |
| 23 | +The Model Context Protocol is an open standard that enables developers to build secure, two-way connections between their data sources and AI-powered tools. With MCP, you can: |
| 24 | + |
| 25 | +- Connect AI applications to your data sources |
| 26 | +- Enable AI tools to access and utilize your data securely |
| 27 | +- Build AI-powered features that have context about your application |
| 28 | + |
| 29 | +For further reading, check out the [Model Context Protocol](https://modelcontextprotocol.io/introduction) website. |
| 30 | + |
| 31 | +## Quickstart with CopilotKit |
| 32 | + |
| 33 | +<Steps> |
| 34 | + <Step> |
| 35 | + ### Get an MCP Server |
| 36 | + First, we need to make sure we have an MCP server to connect to. You can use any MCP SSE endpoint you have configured. |
| 37 | + |
| 38 | + <Accordions className="shadow-lg ring-1 ring-indigo-400 selected bg-gradient-to-r from-indigo-100/80 to-purple-200 dark:from-indigo-900/30 dark:to-purple-900/50"> |
| 39 | + <Accordion title="Get an MCP Server from Composio"> |
| 40 | + <p> |
| 41 | + Composio provides a registry of ready-to-use MCP servers with simple authentication and setup. |
| 42 | + |
| 43 | + To get started, go to [Composio](https://mcp.composio.dev/), find a server the suits your needs and copy the SSE URL before continuing here. |
| 44 | + </p> |
| 45 | + </Accordion> |
| 46 | + </Accordions> |
| 47 | + |
| 48 | + </Step> |
| 49 | + <TailoredContent |
| 50 | + className="step" |
| 51 | + id="cli" |
| 52 | + header={ |
| 53 | + <div> |
| 54 | + <p className="text-xl font-semibold">Use the CopilotKit CLI</p> |
| 55 | + </div> |
| 56 | + } |
| 57 | + > |
| 58 | + <TailoredContentOption |
| 59 | + id="use-cli" |
| 60 | + title="Use the CopilotKit CLI" |
| 61 | + description="I have a Next.js application and want to get started quickly." |
| 62 | + icon={<SquareTerminal />} |
| 63 | + > |
| 64 | + <Step> |
| 65 | + ### Run the CLI |
| 66 | + Just run this following command in your Next.js application to get started! |
| 67 | + |
| 68 | + <Accordions> |
| 69 | + <Accordion title="Don't have a Next.js application?"> |
| 70 | + No problem! Just use `create-next-app` to make one quickly. |
| 71 | + ```bash |
| 72 | + npx create-next-app@latest |
| 73 | + ``` |
| 74 | + </Accordion> |
| 75 | + </Accordions> |
| 76 | + |
| 77 | + ```bash |
| 78 | + npx copilotkit@latest init -m MCP |
| 79 | + ``` |
| 80 | + </Step> |
| 81 | + </TailoredContentOption> |
| 82 | + <TailoredContentOption |
| 83 | + id="do-it-manually" |
| 84 | + title="Code along" |
| 85 | + description="I want to deeply understand what's happening under the hood or don't have a Next.js application." |
| 86 | + icon={<SquareChartGantt />} |
| 87 | + > |
| 88 | + <Step> |
| 89 | + #### Set up the CopilotKit Provider |
| 90 | + |
| 91 | + Wrap your application with the `CopilotKit` provider: |
| 92 | + |
| 93 | + ```tsx |
| 94 | + "use client"; |
| 95 | + |
| 96 | + import { CopilotKit } from "@copilotkit/react-core"; |
| 97 | + |
| 98 | + export default function App() { |
| 99 | + return ( |
| 100 | + <CopilotKit publicApiKey="<replace_with_your_own>"> |
| 101 | + {/* Your app content */} |
| 102 | + </CopilotKit> |
| 103 | + ); |
| 104 | + } |
| 105 | + ``` |
| 106 | + </Step> |
| 107 | + <Step> |
| 108 | + #### Connect to MCP Servers |
| 109 | + |
| 110 | + Create a component to manage MCP server connections: |
| 111 | + |
| 112 | + ```tsx |
| 113 | + "use client"; |
| 114 | + |
| 115 | + import { useCopilotChat } from "@copilotkit/react-core"; |
| 116 | + import { useEffect } from "react"; |
| 117 | + |
| 118 | + function McpServerManager() { |
| 119 | + const { setMcpServers } = useCopilotChat(); |
| 120 | + |
| 121 | + useEffect(() => { |
| 122 | + setMcpServers([ |
| 123 | + { |
| 124 | + // Try a sample MCP server at https://mcp.composio.dev/ |
| 125 | + endpoint: "your_mcp_sse_url", |
| 126 | + }, |
| 127 | + ]); |
| 128 | + }, [setMcpServers]); |
| 129 | + |
| 130 | + return null; |
| 131 | + } |
| 132 | + |
| 133 | + export default McpServerManager; |
| 134 | + ``` |
| 135 | + </Step> |
| 136 | + <Step> |
| 137 | + #### Add the Chat Interface |
| 138 | + |
| 139 | + Add the `CopilotChat` component to your page: |
| 140 | + |
| 141 | + ```tsx |
| 142 | + "use client"; |
| 143 | + |
| 144 | + import { CopilotChat } from "@copilotkit/react-ui"; |
| 145 | + import McpServerManager from "./McpServerManager"; |
| 146 | + |
| 147 | + export default function ChatInterface() { |
| 148 | + return ( |
| 149 | + <div className="flex h-screen p-4"> |
| 150 | + <McpServerManager /> |
| 151 | + <CopilotChat |
| 152 | + instructions="You are a helpful assistant with access to MCP servers." |
| 153 | + className="flex-grow rounded-lg w-full" |
| 154 | + /> |
| 155 | + </div> |
| 156 | + ); |
| 157 | + } |
| 158 | + ``` |
| 159 | + </Step> |
| 160 | + <Step> |
| 161 | + #### Visualize MCP Tool Calls (Optional) |
| 162 | + |
| 163 | + Create a component to display MCP tool calls in your UI: |
| 164 | + |
| 165 | + ```tsx |
| 166 | + "use client"; |
| 167 | + |
| 168 | + import { |
| 169 | + useCopilotAction, |
| 170 | + CatchAllActionRenderProps, |
| 171 | + } from "@copilotkit/react-core"; |
| 172 | + import McpToolCall from "./McpToolCall"; |
| 173 | + |
| 174 | + export function ToolRenderer() { |
| 175 | + useCopilotAction({ |
| 176 | + /** |
| 177 | + * The asterisk (*) matches all tool calls |
| 178 | + */ |
| 179 | + name: "*", |
| 180 | + render: ({ name, status, args, result }: CatchAllActionRenderProps<[]>) => ( |
| 181 | + <McpToolCall status={status} name={name} args={args} result={result} /> |
| 182 | + ), |
| 183 | + }); |
| 184 | + return null; |
| 185 | + } |
| 186 | + ``` |
| 187 | + </Step> |
| 188 | + <Step> |
| 189 | + #### Complete Implementation |
| 190 | + |
| 191 | + Combine all components together: |
| 192 | + |
| 193 | + ```tsx |
| 194 | + "use client"; |
| 195 | + |
| 196 | + import { CopilotKit } from "@copilotkit/react-core"; |
| 197 | + import { CopilotChat } from "@copilotkit/react-ui"; |
| 198 | + import McpServerManager from "./McpServerManager"; |
| 199 | + import { ToolRenderer } from "./ToolRenderer"; |
| 200 | + |
| 201 | + export default function Page() { |
| 202 | + return ( |
| 203 | + <CopilotKit publicApiKey="<replace_with_your_own>"> |
| 204 | + <div className="flex h-screen p-4"> |
| 205 | + <McpServerManager /> |
| 206 | + <CopilotChat |
| 207 | + instructions="You are a helpful assistant with access to MCP servers." |
| 208 | + className="flex-grow rounded-lg w-full" |
| 209 | + /> |
| 210 | + <ToolRenderer /> |
| 211 | + </div> |
| 212 | + </CopilotKit> |
| 213 | + ); |
| 214 | + } |
| 215 | + ``` |
| 216 | + </Step> |
| 217 | + </TailoredContentOption> |
| 218 | + |
| 219 | + </TailoredContent> |
| 220 | +</Steps> |
| 221 | + |
| 222 | +## Advanced Usage |
| 223 | + |
| 224 | +### Implementing the McpToolCall Component |
| 225 | + |
| 226 | +<details> |
| 227 | +<summary>Click to see the McpToolCall component implementation</summary> |
| 228 | + |
| 229 | +```tsx |
| 230 | +"use client"; |
| 231 | + |
| 232 | +import * as React from "react"; |
| 233 | + |
| 234 | +interface ToolCallProps { |
| 235 | + status: "complete" | "inProgress" | "executing"; |
| 236 | + name?: string; |
| 237 | + args?: any; |
| 238 | + result?: any; |
| 239 | +} |
| 240 | + |
| 241 | +export default function MCPToolCall({ |
| 242 | + status, |
| 243 | + name = "", |
| 244 | + args, |
| 245 | + result, |
| 246 | +}: ToolCallProps) { |
| 247 | + const [isOpen, setIsOpen] = React.useState(false); |
| 248 | + |
| 249 | + // Format content for display |
| 250 | + const format = (content: any): string => { |
| 251 | + if (!content) return ""; |
| 252 | + const text = |
| 253 | + typeof content === "object" |
| 254 | + ? JSON.stringify(content, null, 2) |
| 255 | + : String(content); |
| 256 | + return text |
| 257 | + .replace(/\\n/g, "\n") |
| 258 | + .replace(/\\t/g, "\t") |
| 259 | + .replace(/\\"/g, '"') |
| 260 | + .replace(/\\\\/g, "\\"); |
| 261 | + }; |
| 262 | + |
| 263 | + return ( |
| 264 | + <div className="bg-[#1e2738] rounded-lg overflow-hidden w-full"> |
| 265 | + <div |
| 266 | + className="p-3 flex items-center cursor-pointer" |
| 267 | + onClick={() => setIsOpen(!isOpen)} |
| 268 | + > |
| 269 | + <span className="text-white text-sm overflow-hidden text-ellipsis"> |
| 270 | + {name || "MCP Tool Call"} |
| 271 | + </span> |
| 272 | + <div className="ml-auto"> |
| 273 | + <div |
| 274 | + className={`w-2 h-2 rounded-full ${ |
| 275 | + status === "complete" |
| 276 | + ? "bg-gray-300" |
| 277 | + : status === "inProgress" || status === "executing" |
| 278 | + ? "bg-gray-500 animate-pulse" |
| 279 | + : "bg-gray-700" |
| 280 | + }`} |
| 281 | + /> |
| 282 | + </div> |
| 283 | + </div> |
| 284 | + |
| 285 | + {isOpen && ( |
| 286 | + <div className="px-4 pb-4 text-gray-300 font-mono text-xs"> |
| 287 | + {args && ( |
| 288 | + <div className="mb-4"> |
| 289 | + <div className="text-gray-400 mb-2">Parameters:</div> |
| 290 | + <pre className="whitespace-pre-wrap max-h-[200px] overflow-auto"> |
| 291 | + {format(args)} |
| 292 | + </pre> |
| 293 | + </div> |
| 294 | + )} |
| 295 | + |
| 296 | + {status === "complete" && result && ( |
| 297 | + <div> |
| 298 | + <div className="text-gray-400 mb-2">Result:</div> |
| 299 | + <pre className="whitespace-pre-wrap max-h-[200px] overflow-auto"> |
| 300 | + {format(result)} |
| 301 | + </pre> |
| 302 | + </div> |
| 303 | + )} |
| 304 | + </div> |
| 305 | + )} |
| 306 | + </div> |
| 307 | + ); |
| 308 | +} |
| 309 | +``` |
| 310 | + |
| 311 | +</details> |
| 312 | + |
| 313 | +### Self-Hosting Option |
| 314 | + |
| 315 | +<details> |
| 316 | +<summary>Click here to learn how to use MCP with self-hosted runtime</summary> |
| 317 | + |
| 318 | +<Callout type="info" title="Self-Hosting vs Copilot Cloud"> |
| 319 | + The Copilot Runtime handles communication with LLMs, message history, and |
| 320 | + state. You can self-host it or use{" "} |
| 321 | + <LinkToCopilotCloud asButton={false}>Copilot Cloud</LinkToCopilotCloud>{" "} |
| 322 | + (recommended). Learn more in our [Self-Hosting Guide](/guides/self-hosting). |
| 323 | +</Callout> |
| 324 | + |
| 325 | +To configure your self-hosted runtime with MCP servers, you'll need to implement the `createMCPClient` function that matches this interface: |
| 326 | + |
| 327 | +```typescript |
| 328 | +type CreateMCPClientFunction = ( |
| 329 | + config: MCPEndpointConfig |
| 330 | +) => Promise<MCPClient>; |
| 331 | +``` |
| 332 | + |
| 333 | +For detailed implementation guidance, refer to the [official MCP SDK documentation](https://github.com/modelcontextprotocol/typescript-sdk?tab=readme-ov-file#writing-mcp-clients). |
| 334 | + |
| 335 | +Here's a basic example of configuring the runtime: |
| 336 | + |
| 337 | +```tsx |
| 338 | +import { |
| 339 | + CopilotRuntime, |
| 340 | + OpenAIAdapter, |
| 341 | + copilotRuntimeNextJSAppRouterEndpoint, |
| 342 | +} from "@copilotkit/runtime"; |
| 343 | +import { NextRequest } from "next/server"; |
| 344 | + |
| 345 | +const serviceAdapter = new OpenAIAdapter(); |
| 346 | + |
| 347 | +const runtime = new CopilotRuntime({ |
| 348 | + createMCPClient: async (config) => { |
| 349 | + // Implement your MCP client creation logic here |
| 350 | + // See the MCP SDK docs for implementation details |
| 351 | + }, |
| 352 | +}); |
| 353 | + |
| 354 | +export const POST = async (req: NextRequest) => { |
| 355 | + const { handleRequest } = copilotRuntimeNextJSAppRouterEndpoint({ |
| 356 | + runtime, |
| 357 | + serviceAdapter, |
| 358 | + endpoint: "/api/copilotkit", |
| 359 | + }); |
| 360 | + |
| 361 | + return handleRequest(req); |
| 362 | +}; |
| 363 | +``` |
| 364 | + |
| 365 | +</details> |
0 commit comments