|
| 1 | +--- |
| 2 | +title: LangSmith Tool Server |
| 3 | +sidebarTitle: MCP Framework |
| 4 | +mode: wide |
| 5 | +--- |
| 6 | + |
| 7 | +The LangSmith Tool Server is our MCP Framework that powers the tools available in the LangSmith Agent Builder. This framework enables you to build and deploy custom tools that can be integrated with your agents. It provides a standardized way to create, deploy, and manage tools with built-in authentication and authorization. |
| 8 | + |
| 9 | +The PyPi package that defines the framework is available [here](https://pypi.org/project/langsmith-tool-server/). |
| 10 | + |
| 11 | +## Quick start |
| 12 | + |
| 13 | +Install the LangSmith Tool Server and LangChain CLI: |
| 14 | + |
| 15 | +```bash |
| 16 | +pip install langsmith-tool-server |
| 17 | +pip install langchain-cli-v2 |
| 18 | +``` |
| 19 | + |
| 20 | +Create a new toolkit: |
| 21 | + |
| 22 | +```bash |
| 23 | +langchain tools new my-toolkit |
| 24 | +cd my-toolkit |
| 25 | +``` |
| 26 | + |
| 27 | +This creates a toolkit with the following structure: |
| 28 | + |
| 29 | +``` |
| 30 | +my-toolkit/ |
| 31 | +├── pyproject.toml |
| 32 | +├── toolkit.toml |
| 33 | +└── my_toolkit/ |
| 34 | + ├── __init__.py |
| 35 | + ├── auth.py |
| 36 | + └── tools/ |
| 37 | + ├── __init__.py |
| 38 | + └── ... |
| 39 | +``` |
| 40 | + |
| 41 | +Define your tools using the `@tool` decorator: |
| 42 | + |
| 43 | +```python |
| 44 | +from langsmith_tool_server import tool |
| 45 | + |
| 46 | +@tool |
| 47 | +def hello(name: str) -> str: |
| 48 | + """Greet someone by name.""" |
| 49 | + return f"Hello, {name}!" |
| 50 | + |
| 51 | +@tool |
| 52 | +def add(x: int, y: int) -> int: |
| 53 | + """Add two numbers.""" |
| 54 | + return x + y |
| 55 | + |
| 56 | +TOOLS = [hello, add] |
| 57 | +``` |
| 58 | + |
| 59 | +Run the server: |
| 60 | + |
| 61 | +```bash |
| 62 | +langchain tools serve |
| 63 | +``` |
| 64 | + |
| 65 | +Your tool server will start on `http://localhost:8000`. |
| 66 | + |
| 67 | +## Simple client example |
| 68 | + |
| 69 | +Here's a simple example that lists available tools and calls the `add` tool: |
| 70 | + |
| 71 | +```python |
| 72 | +import asyncio |
| 73 | +import aiohttp |
| 74 | + |
| 75 | +async def mcp_request(url: str, method: str, params: dict = None): |
| 76 | + async with aiohttp.ClientSession() as session: |
| 77 | + payload = {"jsonrpc": "2.0", "method": method, "params": params or {}, "id": 1} |
| 78 | + async with session.post(f"{url}/mcp", json=payload) as response: |
| 79 | + return await response.json() |
| 80 | + |
| 81 | +async def main(): |
| 82 | + url = "http://localhost:8000" |
| 83 | + |
| 84 | + tools = await mcp_request(url, "tools/list") |
| 85 | + print(f"Tools: {tools}") |
| 86 | + |
| 87 | + result = await mcp_request(url, "tools/call", {"name": "add", "arguments": {"a": 5, "b": 3}}) |
| 88 | + print(f"Result: {result}") |
| 89 | + |
| 90 | +asyncio.run(main()) |
| 91 | +``` |
| 92 | + |
| 93 | +## Adding OAuth authentication |
| 94 | + |
| 95 | +For tools that need to access third-party APIs (like Google, GitHub, Slack, etc.), you can use OAuth authentication with [Agent Auth](/langsmith/agent-auth). |
| 96 | + |
| 97 | +Before using OAuth in your tools, you'll need to configure an OAuth provider in your LangSmith workspace settings. See the [Agent Auth documentation](/langsmith/agent-auth) for setup instructions. |
| 98 | + |
| 99 | +Once configured, specify the `auth_provider` in your tool decorator: |
| 100 | + |
| 101 | +```python |
| 102 | +from langsmith_tool_server import tool, Context |
| 103 | +from google.oauth2.credentials import Credentials |
| 104 | +from googleapiclient.discovery import build |
| 105 | + |
| 106 | +@tool( |
| 107 | + auth_provider="google", |
| 108 | + scopes=["https://www.googleapis.com/auth/gmail.readonly"], |
| 109 | + integration="gmail" |
| 110 | +) |
| 111 | +async def read_emails(context: Context, max_results: int = 10) -> str: |
| 112 | + """Read recent emails from Gmail.""" |
| 113 | + credentials = Credentials(token=context.token) |
| 114 | + service = build('gmail', 'v1', credentials=credentials) |
| 115 | + # ... Gmail API calls |
| 116 | + return f"Retrieved {max_results} emails" |
| 117 | +``` |
| 118 | + |
| 119 | +Tools with `auth_provider` must: |
| 120 | +- Have `context: Context` as the first parameter |
| 121 | +- Specify at least one scope |
| 122 | +- Use `context.token` to make authenticated API calls |
0 commit comments