# LangChain Integration

> Use Aerostack MCP workspace tools as LangChain DynamicStructuredTool instances. Works with ReAct agents, LangGraph workflows, and any LLM.

Use Aerostack workspace tools as [LangChain](https://js.langchain.com/) tools.
Every MCP tool becomes a `DynamicStructuredTool` — works with ReAct agents, LangGraph workflows, and any LLM.

## Install

```bash npm2yarn
npm install @aerostack/sdk-langchain @langchain/openai @langchain/langgraph
```

## Quick Start

```typescript

const { tools } = await getTools({
  workspace: 'my-workspace',
  token: 'mwt_...',
});

const agent = createReactAgent({
  llm: new ChatOpenAI({ model: 'gpt-4o' }),
  tools,
});

const result = await agent.invoke({
  messages: [{ role: 'user', content: 'Create a GitHub issue for the login bug' }],
});

console.log(result.messages.at(-1)?.content);
```

The SDK:
1. Fetches all MCP tools from your workspace
2. Wraps each as a `DynamicStructuredTool` with name, description, and JSON schema
3. Each tool's `func` property proxies calls through the workspace gateway
4. Errors are returned as string results (not thrown), keeping agent flow intact

## Use Any LLM

LangChain supports many providers. Aerostack tools work with all of them:

```typescript

const { tools } = await getTools({
  workspace: 'my-workspace',
  token: 'mwt_...',
});

// Bind tools directly to a model
const model = new ChatAnthropic({
  model: 'claude-sonnet-4-20250514',
}).bindTools(tools);

const response = await model.invoke('Search Notion for meeting notes');
```

## LangGraph Workflows

Build multi-step workflows with LangGraph:

```typescript

const { tools } = await getTools({
  workspace: 'my-workspace',
  token: 'mwt_...',
});

const agent = createReactAgent({
  llm: new ChatOpenAI({ model: 'gpt-4o' }),
  tools,
});

// Multi-turn conversation
const thread = { configurable: { thread_id: 'my-session' } };

const r1 = await agent.invoke(
  { messages: [{ role: 'user', content: 'Find open GitHub issues labeled "bug"' }] },
  thread,
);

const r2 = await agent.invoke(
  { messages: [{ role: 'user', content: 'Create a Notion page summarizing them' }] },
  thread,
);
```

## Factory Pattern (Recommended for Production)

```typescript

const aerostack = createAerostackLangChain({
  workspace: 'my-workspace',
  token: process.env.AEROSTACK_WORKSPACE_TOKEN!,
});

// Reuse across requests
const { tools } = await aerostack.tools();
```

## What `getTools` Returns

```typescript
const result = await getTools(config);

result.tools  // DynamicStructuredTool[] — pass to agents or model.bindTools()
result.raw    // McpTool[] — raw MCP tool definitions for inspection
```

## Next Steps

- [Create a workspace](/workspaces/create-workspace) and connect MCP servers
- [Browse 250+ MCP servers](/mcp) in the registry
- See the [full API reference](https://github.com/aerostackdev/sdk-langchain) on GitHub
