Configuración de LlamaIndex
Conecte un agente de LlamaIndex al servidor MCP de graph8. El paquete llama-index-tools-mcp de LlamaIndex envuelve las herramientas MCP como FunctionTool nativas de LlamaIndex que sus agentes pueden utilizar.
Requisitos previos
- Python 3.10+
llama-index,llama-index-tools-mcp,llama-index-llms-anthropic(o el proveedor de modelo que prefiera)
pip install llama-index llama-index-tools-mcp llama-index-llms-anthropicMCP alojado (OAuth remoto)
import asyncioimport os
from llama_index.core.agent import ReActAgentfrom llama_index.llms.anthropic import Anthropicfrom llama_index.tools.mcp import McpToolSpec, BasicMCPClient
async def main() -> None: client = BasicMCPClient("https://be.graph8.com/mcp/") tool_spec = McpToolSpec(client=client) tools = await tool_spec.to_tool_list_async()
llm = Anthropic( model="claude-sonnet-4", api_key=os.environ["ANTHROPIC_API_KEY"], ) agent = ReActAgent.from_tools(tools, llm=llm, verbose=True)
response = await agent.achat( "Use g8_find_contacts to preview 10 VP Sales at fintech startups " "in New York. Return name, company, title." ) print(response)
if __name__ == "__main__": asyncio.run(main())La primera llamada abre un navegador para OAuth. Las llamadas posteriores reutilizan el token almacenado en caché.
MCP autoalojado (stdio)
Para uso sin interfaz gráfica o en entornos de CI, ejecute el servidor MCP de forma local con stdio y una clave de API personal.
export G8_API_KEY="g8_..."import asyncioimport os
from llama_index.core.agent import ReActAgentfrom llama_index.llms.anthropic import Anthropicfrom llama_index.tools.mcp import McpToolSpec, BasicMCPClient
async def main() -> None: client = BasicMCPClient( command_or_url="uvx", args=["g8-mcp-server"], env={ "G8_API_KEY": os.environ["G8_API_KEY"], "G8_MCP_MODE": "gtm", }, ) tool_spec = McpToolSpec(client=client) tools = await tool_spec.to_tool_list_async()
llm = Anthropic(model="claude-sonnet-4") agent = ReActAgent.from_tools(tools, llm=llm, verbose=True)
response = await agent.achat("Search my CRM for contacts at Stripe.") print(response)
if __name__ == "__main__": asyncio.run(main())Ejemplo práctico: flujo de prospección SaaS
prompt = ( "Find 25 VP Engineering at Series B SaaS in the US using g8_find_contacts. " "Pick the 10 with the highest signal score. Ask me to confirm before " "saving them to a new list via g8_build_contact_list.")response = await agent.achat(prompt)Las herramientas que consumen créditos requieren confirmación explícita. El servidor MCP devuelve un prompt estructurado que el agente presenta al usuario antes de cualquier operación de guardado.