
ليه الـ tool الكويسة مش مجرد wrapper للـ endpoint؟ إزاي تصمم tools لـ Claude
- كاتب موثّق
- Claude API
محتويات المقال
get_customers بيدوّر على Dana، وجاب كل تاريخ المعاملات بتاعها، وبعدين الـ notes، وبعد حوالي اتناشر call مليانين UUIDs قال إن فيه refund طلع إنه بتاع customer تاني. كل endpoint اشتغل صح. والـ tools برضه فشلت.
الفكرة الغلط الشائعة
الـ client library بتقلّد الـ API اللي بتناديه، فـ tool set بيقلّد الـ API شكله صح بنفس الدرجة. الـ generators بتحوّل ملف OpenAPI لـ tools في دقايق، والـ coverage الكاملة بتدّي إحساس إن الشغل متقن. الـ endpoints أصلًا متيستة ومتوثقة وعليها permissions، فإعادة استخدامها شكلها الأأمن. tools أكتر بتبان كأنها قدرات أكتر.
اللي بيحصل فعلًا
الـ tool عقد مع caller مش deterministic
الـ context هو المورد النادر
list_contacts بتجبر الـ agent يقرا الـ contacts واحد واحد، كأنه بيقرا الـ address book من أول صفحة. إنما tool زي search_contacts بتخليه ينط للصفحة الصح. وأي حاجة الـ tool بترجعها بتفضل في المحادثة، وبتتبعت تاني مع كل turn بعدها.
صمّم الـ tools حوالين المهام، مش الـ routes
schedule_eventاللي بتلاقي المواعيد الفاضية وتحجز، بدلlist_usersوlist_eventsوcreate_event.search_logsاللي بترجع السطور المهمة ومعاها شوية context، بدلread_logs.get_customer_contextاللي بتجمّع المعلومات الحديثة والمهمة عن الـ customer، بدلget_customer_by_idوlist_transactionsوlist_notes.
github_list_prs.
رجّع اللي الخطوة الجاية محتاجاه
uuid و256px_image_url وmime_type، وتحط مكانها name وimage_url وfile_type، وبيقول إن تحويل الـ UUIDs العشوائية لأسماء ليها معنى "significantly improves Claude's precision in retrieval tasks by reducing hallucinations". ولو call بعد كده محتاج ID، الـ parameter اللي اسمه response_format بقيمتين concise وdetailed بيخلّي الـ agent يطلبه. والنتايج الكبيرة ياخدوا pagination أو filtering أو truncation بـ defaults معقولة، والـ errors لازم تقول المفروض يتغير إيه بدل code مبهم أو traceback.
الـ CRM قبل وبعد
# Generated from openapi.yaml: one tool per endpointTOOLS = [ {"name": "get_customers", "description": "GET /customers", "input_schema": {"type": "object", "properties": {"page": {"type": "integer"}}}}, {"name": "get_customer_transactions", "description": "GET /customers/{id}/transactions", "input_schema": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]}}, {"name": "get_customer_notes", "description": "GET /customers/{id}/notes", "input_schema": {"type": "object", "properties": {"id": {"type": "string"}}, "required": ["id"]}}, # ...and one more for every other route in the spec]CUSTOMER_CONTEXT_TOOL = { "name": "crm_get_customer_context", "description": ( "Finds one customer by name or email and returns their profile, " "last 20 transactions and open support notes in a single call. Use " "it before answering any question about a specific customer's " "billing or history. If several customers match, it returns up to " "5 candidates instead, so ask the user which one they mean. Set " "response_format to 'detailed' only when a next step needs IDs." ), "input_schema": { "type": "object", "properties": { "customer": {"type": "string", "description": "Full name or email address"}, "response_format": {"type": "string", "enum": ["concise", "detailed"]}, }, "required": ["customer"], },}def crm_get_customer_context(customer: str, response_format="concise"): matches = crm.search_customers(customer, limit=5) # never the full list if len(matches) != 1: found = "; ".join(f"{m.name} <{m.email}>" for m in matches) or "none" return f"{len(matches)} matches for {customer!r}: {found}. Ask which." c = matches[0] lines = [f"{c.name} <{c.email}>, customer since {c.since}"] lines += [f"{t.date} {t.amount} {t.status}" for t in crm.list_transactions(c.id, limit=20)] lines += [f"note: {n.text}" for n in crm.list_notes(c.id, status="open")] if response_format == "detailed": # IDs only when a next call needs them lines.append(f"customer_id: {c.id}") return "\n".join(lines)print(crm_get_customer_context("Dana Levi"))تطبقها إزاي في مشروعك
اعمل list بالمهام، مش بالـ endpoints اجمع الأسئلة اللي الناس بتسألها للـ assistant بتاعك فعلًا، من الـ logs أو الـ support tickets، وصمّم tool لكل workflow بيتكرر كتير. اجمع السلاسل لو الـ agent دايمًا بينادي تلات tools ورا بعض، اعمل tool واحدة بتعمل التلاتة على الـ API بتاعك. بدّل الـ lists بـ search ادّي كل collection إمكانية search بـ filters، وlimit صغير افتراضي، وتلميح لما النتايج تتقطع. خفّف الـ responses وخليها مفهومة رجّع الأسماء والـ fields اللي الخطوة الجاية محتاجاها، واعرض الـ IDs عن طريق parameter زي response_format.اكتب الـ descriptions لموظف جديد اشرح الـ tool بتعمل إيه، وتتستخدم امتى، وكل parameter معناه إيه، وإيه اللي مش هترجعه، واختار أسماء parameters واضحة زي user_idبدلuser.
الخلاصة
الـ caller بتاع الـ tool موديل بيقرا كل token في التعريفات والـ responses، مش program ذاكرته رخيصة. إنك تنادي الـ API بتاعك جوه tool عادي؛ الغلط إنك تعمل tool لكل endpoint. اجمع الـ calls اللي بتتربط ورا بعض في tools على شكل مهام، وبدّل الـ list endpoints بـ search. رجّع fields مهمة وأسماء مقروءة، والـ IDs والتفاصيل بس لما تتطلب. اوصف كل tool كأنك بتشرح لزميل جديد، وبعدين حسّنها قصاد evaluation.
التعليقات (0)