Skip to content

Commit 2be74c8

Browse files
feat(anthropic): support for mcp toolset tool
1 parent f39cebc commit 2be74c8

File tree

9 files changed

+549
-8
lines changed

9 files changed

+549
-8
lines changed

.changeset/weak-parents-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@langchain/anthropic": patch
3+
---
4+
5+
add named mcp toolset tool

libs/providers/langchain-anthropic/README.md

Lines changed: 140 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -525,25 +525,21 @@ Available commands:
525525
- Restart the session: `{ restart: true }`
526526

527527
```typescript
528-
import {
529-
ChatAnthropic,
530-
tools,
531-
type Bash20250124Command,
532-
} from "@langchain/anthropic";
528+
import { ChatAnthropic, tools } from "@langchain/anthropic";
533529
import { execSync } from "child_process";
534530

535531
const llm = new ChatAnthropic({
536532
model: "claude-sonnet-4-5-20250929",
537533
});
538534

539535
const bash = tools.bash_20250124({
540-
execute: async (args: Bash20250124Command) => {
536+
execute: async (args) => {
541537
if (args.restart) {
542538
// Reset session state
543539
return "Bash session restarted";
544540
}
545541
try {
546-
const output = execSync(args.command!, {
542+
const output = execSync(args.command, {
547543
encoding: "utf-8",
548544
timeout: 30000,
549545
});
@@ -567,6 +563,143 @@ console.log(response.tool_calls?.[0].args.command); // "ls -la *.py"
567563

568564
For more information, see [Anthropic's Bash Tool documentation](https://docs.anthropic.com/en/docs/agents-and-tools/tool-use/bash-tool).
569565

566+
### MCP Toolset
567+
568+
The MCP toolset (`mcpToolset_20251120`) enables Claude to connect to remote MCP (Model Context Protocol) servers directly from the Messages API without implementing a separate MCP client. This allows Claude to use tools provided by MCP servers.
569+
570+
Key features:
571+
572+
- **Direct API integration** - Connect to MCP servers without implementing an MCP client
573+
- **Tool calling support** - Access MCP tools through the Messages API
574+
- **Flexible tool configuration** - Enable all tools, allowlist specific tools, or denylist unwanted tools
575+
- **Per-tool configuration** - Configure individual tools with custom settings
576+
- **OAuth authentication** - Support for OAuth Bearer tokens for authenticated servers
577+
- **Multiple servers** - Connect to multiple MCP servers in a single request
578+
579+
```typescript
580+
import { ChatAnthropic, tools } from "@langchain/anthropic";
581+
582+
const llm = new ChatAnthropic({
583+
model: "claude-sonnet-4-5-20250929",
584+
});
585+
586+
// Basic usage - enable all tools from an MCP server
587+
const response = await llm.invoke("What tools do you have available?", {
588+
mcp_servers: [
589+
{
590+
type: "url",
591+
url: "https://example-server.modelcontextprotocol.io/sse",
592+
name: "example-mcp",
593+
authorization_token: "YOUR_TOKEN",
594+
},
595+
],
596+
tools: [tools.mcpToolset_20251120({ serverName: "example-mcp" })],
597+
});
598+
```
599+
600+
**Allowlist pattern** - Enable only specific tools:
601+
602+
```typescript
603+
const response = await llm.invoke("Search for events", {
604+
mcp_servers: [
605+
{
606+
type: "url",
607+
url: "https://calendar.example.com/sse",
608+
name: "google-calendar-mcp",
609+
authorization_token: "YOUR_TOKEN",
610+
},
611+
],
612+
tools: [
613+
tools.mcpToolset_20251120({
614+
serverName: "google-calendar-mcp",
615+
// Disable all tools by default
616+
defaultConfig: { enabled: false },
617+
// Explicitly enable only these tools
618+
configs: {
619+
search_events: { enabled: true },
620+
create_event: { enabled: true },
621+
},
622+
}),
623+
],
624+
});
625+
```
626+
627+
**Denylist pattern** - Disable specific tools:
628+
629+
```typescript
630+
const response = await llm.invoke("List my events", {
631+
mcp_servers: [
632+
{
633+
type: "url",
634+
url: "https://calendar.example.com/sse",
635+
name: "google-calendar-mcp",
636+
authorization_token: "YOUR_TOKEN",
637+
},
638+
],
639+
tools: [
640+
tools.mcpToolset_20251120({
641+
serverName: "google-calendar-mcp",
642+
// All tools enabled by default, just disable dangerous ones
643+
configs: {
644+
delete_all_events: { enabled: false },
645+
share_calendar_publicly: { enabled: false },
646+
},
647+
}),
648+
],
649+
});
650+
```
651+
652+
**Multiple MCP servers**:
653+
654+
```typescript
655+
const response = await llm.invoke("Use tools from both servers", {
656+
mcp_servers: [
657+
{
658+
type: "url",
659+
url: "https://mcp.example1.com/sse",
660+
name: "mcp-server-1",
661+
authorization_token: "TOKEN1",
662+
},
663+
{
664+
type: "url",
665+
url: "https://mcp.example2.com/sse",
666+
name: "mcp-server-2",
667+
authorization_token: "TOKEN2",
668+
},
669+
],
670+
tools: [
671+
tools.mcpToolset_20251120({ serverName: "mcp-server-1" }),
672+
tools.mcpToolset_20251120({
673+
serverName: "mcp-server-2",
674+
defaultConfig: { deferLoading: true },
675+
}),
676+
],
677+
});
678+
```
679+
680+
**With Tool Search** - Use deferred loading for on-demand tool discovery:
681+
682+
```typescript
683+
const response = await llm.invoke("Find and use the right tool", {
684+
mcp_servers: [
685+
{
686+
type: "url",
687+
url: "https://example.com/sse",
688+
name: "example-mcp",
689+
},
690+
],
691+
tools: [
692+
tools.toolSearchRegex_20251119(),
693+
tools.mcpToolset_20251120({
694+
serverName: "example-mcp",
695+
defaultConfig: { deferLoading: true },
696+
}),
697+
],
698+
});
699+
```
700+
701+
For more information, see [Anthropic's MCP Connector documentation](https://docs.anthropic.com/en/docs/agents-and-tools/mcp-connector).
702+
570703
## Development
571704

572705
To develop the Anthropic package, you'll need to follow these instructions:

libs/providers/langchain-anthropic/src/chat_models.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ import {
5757
AnthropicToolChoice,
5858
ChatAnthropicOutputFormat,
5959
ChatAnthropicToolType,
60+
AnthropicMCPServerURLDefinition,
6061
Kwargs,
6162
} from "./types.js";
6263
import { wrapAnthropicClientError } from "./utils/errors.js";
@@ -115,6 +116,10 @@ export interface ChatAnthropicCallOptions
115116
* See https://docs.anthropic.com/en/api/versioning for available beta features.
116117
*/
117118
betas?: AnthropicBeta[];
119+
/**
120+
* Array of MCP server URLs to use for the request.
121+
*/
122+
mcp_servers?: AnthropicMCPServerURLDefinition[];
118123
}
119124

120125
function _toolsInParams(
@@ -168,12 +173,13 @@ function isBuiltinTool(tool: unknown): tool is AnthropicBuiltInToolUnion {
168173
"code_execution_",
169174
"memory_",
170175
"tool_search_",
176+
"mcp_toolset",
171177
];
172178
return (
173179
typeof tool === "object" &&
174180
tool !== null &&
175181
"type" in tool &&
176-
"name" in tool &&
182+
("name" in tool || "mcp_server_name" in tool) &&
177183
typeof tool.type === "string" &&
178184
builtInToolPrefixes.some(
179185
(prefix) => typeof tool.type === "string" && tool.type.startsWith(prefix)
@@ -1062,6 +1068,7 @@ export class ChatAnthropicMessages<
10621068
container: options?.container,
10631069
betas: _combineBetas(this.betas, options?.betas, toolBetas ?? []),
10641070
output_format: options?.output_format,
1071+
mcp_servers: options?.mcp_servers,
10651072
};
10661073

10671074
if (this.thinking.type === "enabled") {

libs/providers/langchain-anthropic/src/tools/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { textEditor_20250728 } from "./textEditor.js";
99
import { computer_20251124, computer_20250124 } from "./computer.js";
1010
import { codeExecution_20250825 } from "./codeExecution.js";
1111
import { bash_20250124 } from "./bash.js";
12+
import { mcpToolset_20251120 } from "./mcpToolset.js";
1213

1314
export const tools = {
1415
memory_20250818,
@@ -21,6 +22,7 @@ export const tools = {
2122
computer_20250124,
2223
codeExecution_20250825,
2324
bash_20250124,
25+
mcpToolset_20251120,
2426
};
2527

2628
export type * from "./types.js";

0 commit comments

Comments
 (0)