This commit is contained in:
23
docs/snippets/python/mcp/mcp_custom_client.md
Normal file
23
docs/snippets/python/mcp/mcp_custom_client.md
Normal file
@@ -0,0 +1,23 @@
|
||||
```python title="Python"
|
||||
import asyncio
|
||||
from mcp import ClientSession, StdioServerParameters
|
||||
from mcp.client.stdio import stdio_client
|
||||
|
||||
async def main() -> None:
|
||||
server_params: StdioServerParameters = StdioServerParameters(
|
||||
command="kreuzberg", args=["mcp"]
|
||||
)
|
||||
|
||||
async with stdio_client(server_params) as (read, write):
|
||||
async with ClientSession(read, write) as session:
|
||||
await session.initialize()
|
||||
tools = await session.list_tools()
|
||||
tool_names: list[str] = [t.name for t in tools.tools]
|
||||
print(f"Available tools: {tool_names}")
|
||||
result = await session.call_tool(
|
||||
"extract_file", arguments={"path": "document.pdf", "async": True}
|
||||
)
|
||||
print(result)
|
||||
|
||||
asyncio.run(main())
|
||||
```
|
||||
36
docs/snippets/python/mcp/mcp_langchain_integration.md
Normal file
36
docs/snippets/python/mcp/mcp_langchain_integration.md
Normal file
@@ -0,0 +1,36 @@
|
||||
```python title="Python"
|
||||
from langchain.agents import initialize_agent, AgentType
|
||||
from langchain.tools import Tool
|
||||
from langchain_openai import ChatOpenAI
|
||||
import subprocess
|
||||
import json
|
||||
|
||||
mcp_process = subprocess.Popen(
|
||||
["kreuzberg", "mcp"],
|
||||
stdin=subprocess.PIPE,
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
|
||||
def extract_file(path: str) -> str:
|
||||
request: dict = {
|
||||
"method": "tools/call",
|
||||
"params": {
|
||||
"name": "extract_file",
|
||||
"arguments": {"path": path, "async": True},
|
||||
},
|
||||
}
|
||||
mcp_process.stdin.write(json.dumps(request).encode() + b"\n")
|
||||
mcp_process.stdin.flush()
|
||||
response = mcp_process.stdout.readline()
|
||||
return json.loads(response)["result"]["content"]
|
||||
|
||||
tools: list[Tool] = [
|
||||
Tool(name="extract_document", func=extract_file, description="Extract")
|
||||
]
|
||||
|
||||
llm = ChatOpenAI(temperature=0)
|
||||
agent = initialize_agent(
|
||||
tools, llm, agent=AgentType.ZERO_SHOT_REACT_DESCRIPTION
|
||||
)
|
||||
```
|
||||
17
docs/snippets/python/mcp/mcp_server_start.md
Normal file
17
docs/snippets/python/mcp/mcp_server_start.md
Normal file
@@ -0,0 +1,17 @@
|
||||
```python title="Python"
|
||||
import subprocess
|
||||
import time
|
||||
from typing import Optional
|
||||
|
||||
mcp_process: subprocess.Popen = subprocess.Popen(
|
||||
["python", "-m", "kreuzberg", "mcp"],
|
||||
stdout=subprocess.PIPE,
|
||||
stderr=subprocess.PIPE,
|
||||
)
|
||||
|
||||
pid: Optional[int] = mcp_process.pid
|
||||
print(f"MCP server started with PID: {pid}")
|
||||
|
||||
time.sleep(1)
|
||||
print("Server is running, listening for connections")
|
||||
```
|
||||
Reference in New Issue
Block a user